anijitsao / simple-aws-lambda-mongodb

Simple REST / HTTP API using Node JS, AWS Lambda, AWS SDK JavaScript and MongoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

simple-aws-lambda-mongodb

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.

Features

  1. AWS Lambda function using NodeJS
  2. Function is using latest version of AWS SDK JavaScript v3 with all ES6+ syntaxes like Promises, async/await
  1. Function are deployed using Serverless Framework.
  2. serverless.json is used for deployment configuration instead of serverless.yml.
  3. All the deployment is created in AWS S3 to store the .zip of the function code and AWS CloudFormation Stack.
  1. For session tracking JWT is used.
  2. AWS HTTP API are using AWS API GateWay
  1. All data is saved in MongoDB Atlas i.e. persistent
  2. Caching is used for faster response in the APIs. Redis is used for that purpose
  3. This APIs can also be consumed by any Frontend Application.
  1. NPM dependencies are used for various purposes.
  2. Custom Headers are added with the response for obvious security reasons.

Usage

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

Deployment

In order to deploy the example, you need to run the following command:

$ serverless deploy

Invocation

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.

API Listing

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
  }
}  

About

Simple REST / HTTP API using Node JS, AWS Lambda, AWS SDK JavaScript and MongoDB


Languages

Language:JavaScript 100.0%