Coder Social home page Coder Social logo

s-polimetla / lambda-dynamodb-api-sam Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 3.0 174 KB

Cloud Native RESTful web service coded with Node.js using AWS SAM template, CodePipeline, Lambda and DynamoDB

Home Page: https://medium.com/twodigits/repatriation-from-microservices-on-cloud-to-on-premise-monolith-ab61d293dfaa

License: MIT License

Shell 4.64% JavaScript 95.36%
aws-serverless-application-model dynamodb aws-lambda nodejs swagger cloudformation no-sql serverless codepipeline ci-cd cloudnative apigateway json-schema schema-validation

lambda-dynamodb-api-sam's Introduction

lambda-dynamodb-api-sam

A micro service which reads, writes and updates data in a DynamoDB table. Built with AWS SDK, SAM template,swagger and coded with Node.js.

Disclaimer - This project should be considered a POC or a tutorial. If you decided to run this on production systems you do so at your own risk.

Data model

The DynamoDB table holds information about devices. Each item of the table has following structure

{
    "deviceId": "generated-uuid-string",
    "deviceInfo": {
      "firmwareRevision": "string",
      "firmwareVersion": "string",
      "name": "string"
    },
    "lastUpdated": "generated-timestamp-string"
}

Deployment Work Flow

The pipeline is created using a build-serverless-pipeline. Simply create a stack with this file via CloudFormation Console or using CLI. This file is generic and can be used for similar purposes.

  1. The value of ProjectName should be the same as Service in the Parameters mentioned.
  2. The name of the S3 Bucket mentioned in the config.json should be the same as the one created by the build-pipeline stack.
  3. Uploading build-serverless-pipeline creates a stack which creates a CodePipeline
  4. This pipeline listens to changes done to the master branch. Upon every commit the code is fetched into the pipeline in the first phase of the pipeline Source
  5. In the step step namely, Build - the dependencies are installed.
  6. In my implementation, I am also running unit tests and linter as a way to ensure well tested implementation and to enforce clean code principles. To skip this step, you may comment out the respective lines of code in build.sh
  7. During the Deploy stage of the pipeline, a change set is first created which validates template.yaml first before creating or updating resources. This can be skipped as well but I would recommend to keep this as it would inform you about any errors with template.yaml right away than throwing errors while creating the stack.
  8. In the second step of Deploy Stage, another CloudFormation stack is created based on template.yaml. This creates all the resources mentioned in the template. Please note, that I have referred to swagger.yaml in the template.yaml to create API Gateway since it is a elegant way and also to enforce security features and request validation. You may chose to map Events to your lambda function if you choose not to use swagger.
  9. SAM currently does not support creation of Usage Plans which are useful to secure your API with a API-Key. Refer Feature-Request. This can be created either by traditional CloudFormation configuration or manually.
  10. I have created them manually in my implementation (hardly 5 min work). Create a API Key and Usage Plan. Attach the API Key to a Usage Plan which should be attached to stage of the API created. (In my case dev)

Schema Validation for API requests

The requests are validated at the API level before the lambda function is triggered. This narrows down the possibility of ill-formed data entering via POST request.

Example:

If the POST body has

   {
      "firmwareRevision": 23,
      "firmwareVersion": "testVersion",
      "name": "testName"
    }

The above request would fail before reaching lambda since firmwareVersion is defined as a string and not integer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.