Going Serverless: AWS Lambda, and S3

Standard
Reading Time: < 1 minute

Setting up a local development environment for a serverless AWS system. I will be setting up a local development environment.

Things needed:
NVM
Node
AWS CLI tool Version 1
Serverless Framework

Install AWS CLI Version 1:

The guide to install can be found here.

Install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash

Install Node:

nvm install 10

nvm use 10

Install Serverless Framework:

npm install -g serverless

Create Project:

serverless create --template aws-nodejs --path projectName
cd projectName

Initialize NPM:

npm init

Node packages we’ll be using:

npm install aws-sdk --save-dev
npm install serverless-offline --save-dev
npm install serverless-http -save-dev
npm install serverless-s3-local --save-dev

AWS Profile (~/.aws/credentials):

[s3local]
aws_access_key_id = S3RVER
aws_secret_access_key = S3RVER

Using the profile:

export AWS_PROFILE=s3local

Testing and Using S3

Configure: (serverless.yml)

plugins:
  - serverless-s3-local
  - serverless-offline

custom:
  s3:
    port: 8081
    directory: ./tmp

resources:
  Resources:
    NewResources:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: local-bucket

...

functions:
  s3hook:
    handler: handler.s3hook
    events:
      - s3: local-bucket

Edit handler.js:

...
module.exports.s3hook = async (event, context) => {
  console.log(JSON.stringify(event));
};

Start Serverless Offline: (try to use separate terminals)

sls offline start

Pushing up file to Local S3:

aws --endpoint http://localhost:8081 s3api put-object --bucket local-bucket --key handler.js --body ./handler.js

Retrieving a file from Local S3:

aws --endpoint http://localhost:8081 s3api get-object --bucket local-bucket --key handler.js ./tmp/handler.js

Results:

On your second terminal, the one you ran sls offline start you should have log outputs stating you have successful PUT and GET responses.

For the GET request, you should have a file in your tmp/ directory.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.