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:
0 1 2 |
<code>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash</code> |
Install Node:
0 1 2 3 4 |
<code>nvm install 10 nvm use 10</code> |
Install Serverless Framework:
0 1 2 |
<code>npm install -g serverless</code> |
Create Project:
0 1 2 3 |
<code>serverless create --template aws-nodejs --path projectName cd projectName</code> |
Initialize NPM:
0 1 2 |
<code>npm init</code> |
Node packages we’ll be using:
0 1 2 3 4 5 |
<code>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</code> |
AWS Profile (~/.aws/credentials):
0 1 2 3 4 |
<code>[s3local] aws_access_key_id = S3RVER aws_secret_access_key = S3RVER</code> |
Using the profile:
0 1 2 |
<code>export AWS_PROFILE=s3local</code> |
Testing and Using S3
Configure: (serverless.yml)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<code>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</code> |
Edit handler.js:
0 1 2 3 4 5 6 |
<code>... module.exports.s3hook = async (event, context) => { console.log(JSON.stringify(event)); }; </code> |
Start Serverless Offline: (try to use separate terminals)
0 1 2 |
<code>sls offline start</code> |
Pushing up file to Local S3:
0 1 2 |
<code>aws --endpoint http://localhost:8081 s3api put-object --bucket local-bucket --key handler.js --body ./handler.js</code> |
Retrieving a file from Local S3:
0 1 2 |
<code>aws --endpoint http://localhost:8081 s3api get-object --bucket local-bucket --key handler.js ./tmp/handler.js</code> |
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.