This example demonstrates how to deploy a NodeJS function running on AWS Lambda using the traditional Serverless Framework. The deployed functions work with MongoDB Atlas.
This Example works with AWS HTTP API events. For faster response with the APIs Redis caching is used. For session tracking JSON Web Token (JWT) is used.
All logs for the function is kept in AWS Cloudwatch i.e persistent.
To use the code in this example you must have an valid AWS account and necessary AWS IAM roles and programmatic access to an user. You must have a MongoDB Atlas account as well as an AWS EC2 instance should be installed with Redis.
User Sticker App is related to this application.
- AWS Lambda function using NodeJS
- Function is using latest version of AWS SDK JavaScript v3 with all ES6+ syntaxes like Promises,
async/await
- Function are deployed using Serverless Framework.
-
serverless.json
is used for deployment configuration instead ofserverless.yml
. -
All the deployment is created in AWS S3 to store the
.zip
of the function code and AWS CloudFormation Stack.
- For session tracking JWT is used.
- AWS HTTP API are using AWS API GateWay
- All data is saved in MongoDB Atlas i.e. persistent
- Caching is used for faster response in the APIs. Redis is used for that purpose
- This APIs can also be consumed by any Frontend Application.
- NPM dependencies are used for various purposes.
- Custom Headers are added with the response for obvious security reasons.
First clone the repo
$ git clone https://github.com/anijitsao/simple-aws-lambda-mongodb.git
Install all the necessary dependencies by going inside the directory
$ cd simple-aws-lambda-mongodb
$ npm install
In order to deploy the example, you need to run the following command:
$ serverless deploy
After successful deployment, you can invoke the deployed function.
All the cron
events will invoke the deployed functions in stipulated time interval.
However, to call using httpApi
you can use any REST Client like Talend API Tester with the url
and HTTP Verbs as shown in Terminal after using serverless deploy
.
POST /login have the following input json
{
"username": "arisgh7"
}
Following APIs must contain Authorization: Bearer <token>
in the headers
POST /adduser have the following input json
{
"name": "John Doe",
"designation": "Actor"
}
GET /getallusers have no input json
DELETE /deleteuser?recordId=record-id-to-delete have no input json. But it should contain the record id to be deleted in the Query string
PUT /updateuser have the following input json
{
"updateId": "id-of-the-record-to-be-updated",
"updateDoc": {
"name": "Arijit Singh",
"designation": "Singer",
"country": "India" // this field is optional
}
}