- NodeJs
- Serverless Framework CLI
- AWS CLI
- Active AWS account with ApiGateway and Dynamo db access
$node -v
Install serverless framework:
$npm install serverless -g
$serverless -v (or) sls -v
$aws --version
$aws configure
AWS Access Key ID [None]: <<Input here>>
AWS Secret Access Key [None]: <<Input here>>
Default region name [None]:
Default output format [None]:
Please follow these steps while creating the AWS IAM User.
- Go to the Users section under Identity and Access Management (IAM) services in AWS management console.
- Click Add User
- Enter a valid user name. Under
Select AWS access type
check the box forProgrammatic access
. - Select
Attach existing policies directly
underSet permissions
on theAdd User
screen. - Attach the below listed policies to the IAM User.
- Standard AWS Managed Policies that you need to attach to your user.
AmazonDynamoDBFullAccess
AmazonAPIGatewayAdministrator
AWSLambdaFullAccess
- Hit
Next:Tags
. Skip toNext:Review
- Review the User permissions and policies attached. Hit
Create user
. - From the confirmation screen please download and save the
ACCESS_KEY_ID
andSECRET_ACCESS_KEY
at a secure location.
To run the project locally, you will need the following secrets from your AWS IAM User.
ACCESS_KEY_ID
SECRET_ACCESS_KEY
- Grab the
ACCESS_KEY_ID
andSECRET_ACCESS_KEY
for the above created IAM User and replace it in the below command respectively.
$ node_modules/.bin/serverless config credentials --provider aws --key <ACCESS_KEY_ID> --secret <SECRET_ACCESS_KEY>
This will create ~/.aws/credentials
in your computer. serverless
will use this credentials to deploy to your account
Serverless framework application model:
-
Functions: which executes some code in response to some events.
-
Events: Like HttpRequest, a schedule timer event or infrastructure provider event like aws s3 fileupload event
In AWS, you can trigger functions with the events from API Gateway, s3, CloudWatch, DynamoDb
-
Resources: Infrastructure that your application depends on ex: dynamodb table, aws s3 bucket etc.
-
Serverless Services: A service is like a project. It's where you define your AWS Lambda functions, the events that trigger them and any AWS Infrastructure resources they require, all in a file called serverless.yml. A fullblown application can have many services.
Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST and WebSocket APIs at any scale.
- Amazon Dynamodb is a fully managed NOSQL database service that provides fast and predictable performance with seamless scalability.
git clone git@github.com:srigumm/serverless-nodejs-aws-lambda-demo.git
cd <<>>
npm install
aws config
**configure aws access key here
serverless deploy
Curl -i -H "Content-Type: application/json" -X POST -d '{"flavour":"Strawberry","quantity":22}' https://<<your gateway url>>/dev/api/v1/ecupcakes/order
Curl -i -H "Content-Type: application/json" -X POST -d '{"flavour":"Strawberry","quantity":11}' https://<<your gateway url>>/dev/api/v1/ecupcakes/order
Curl -i -H "Content-Type: application/json" -X GET https://<<your gateway url>>/dev/api/v1/ecupcakes/order
serverless remove
serverless create --template aws-nodejs --path <<servicename>>
This should create "serverless.yml" and "handler.js" file in your current directory
serverless deploy
(or)
serverless deploy function --function <<functionamehere>>
(or)
serverless deploy --package package-path
Inspired by - https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb