[Advanced] - Using AWS-SDK to Create and List S3 Bucket
- Stands for simple storage service
- One of the first services in AWS
- S3 is a safe place to store any type of files (text files, images, mp3)
- You can think of s3 as Dropbox or GoogleDrive
- Is Object based(Key & Value pair)
- FIles are stored in buckets(similar to folder)
- Bucket Names must be unique
- Software Development Kit
- A collection of tools for developers creating web applications provided by aws
- Provides APIs, class libraries, and code samples
- We will be using aws-sdk to communicate with s3
- After creating your serverless boilerplate/template run
npm init --yes
- This will create a package.json file for you
- package.json will allow us to install aws-sdk npm pakaage to use
- To install aws-sdk run
npm install aws-sdk
- You should now see a
node_modules
directory in your current directory
- Require aws-sdk and create an s3 client in our handler.js file
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
Create Bucket - AWS SDK for JavaScript
s3.createBucket({ Bucket: "YOUR_BUCKET_NAME"}, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
- Hard code YOUR_BUCKET_NAME for now(We will add dynamically through Postman)
- Remove
async
keyword from lambda function - Add
callback
parameter in lambda function - Store return vaule in a variable named
response
- Pass
response
into callback functioncallback(null, response)
- Run lambda function locally
- Pass in bucket name data dynamically
- Create a
dummyData.json
file. Add the following:
{
"bucketName": "testingjay123"
}
- run with --path to pass in
dummyData.json
serverless invoke local --path ./data.json
- Before deploying lambda function, lets add a few code snippets to our
serverless.yml
file - Add
iamRoleStatements:
to the end ofprovider:
:
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-west-2
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
Resource: "*"
- This allows our deployed lambda function to access the s3 service
- Add
integration: lambda
undercors: true
functions:
create:
handler: handler.create
events:
- http:
path: create
method: post
cors: true
integration: lambda
- This allows us to recieve data passed in through Postman
- Deploy serverless
- Copy and paste the returned POST endpoint url and paste into Postman Post method
- In Postman, select
Body
->raw
-> selectJSON(application/json)
from the dropdown that saysText
- In the below input field, type in JSON data format exactly like the
dummyData.json
file
- Create new directory
lambdas
- Rename
handler.js
to lambda namecreate.js
- Move existing lambda function
create.js
to 'lambdas' directory - Change
module.exports.create
tomodule.exports.main
in yourcreate.js
file - Change
handler
belowfunctions
tocreate
- change value of
handler
property to lambdas/create.main
functions:
create:
handler: lambdas/create.main
- Make a
GET
request to get All Buckets data - Look through AWS SDK for JavaScript documentation to find the correct method
AWS.S3 — AWS SDK for JavaScript