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:
<code>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash</code>
Install Node:
<code>nvm install 10
nvm use 10</code>
Install Serverless Framework:
<code>npm install -g serverless</code>
Create Project:
<code>serverless create --template aws-nodejs --path projectName
cd projectName</code>
Initialize NPM:
<code>npm init</code>
Node packages we’ll be using:
<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):
<code>[s3local]
aws_access_key_id = S3RVER
aws_secret_access_key = S3RVER</code>
Using the profile:
<code>export AWS_PROFILE=s3local</code>
Testing and Using S3
Configure: (serverless.yml)
<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:
<code>...
module.exports.s3hook = async (event, context) => {
console.log(JSON.stringify(event));
};
</code>
Start Serverless Offline: (try to use separate terminals)
<code>sls offline start</code>
Pushing up file to Local S3:
<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:
<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.