sankar2389 / sample-serverless-node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of contents

Sample serverless node

Introduction

This node application is used to demonstrate

  • Building microservices on AWS
  • Serverless backend
  • Communication between services using
    • Direct http calls
    • Message queue (SQS)
    • Near real-time data streaming using AWS Kinesis
  • Infrastructure as code (defined in serverless.yaml)
  • Integration tests using supertest

Tech stack

  • AWS Lambda + with IAM role
  • Amazon SQS
  • Amazon Kinesis
  • AWS Cloudformation
  • supertest library for integration test
  • AWS DynamoDB
  • PostgreSQL
  • Docker

Code structures

  • At root directory: contains docker-compose.yaml for local development environment. This contains several images
    • postgres database
    • sqs
    • kinesis
    • dynamodb
  • Each service is separated into its own folder and contains
    • serverless.yaml: define service resource using Cloudformation syntax
    • tests: unit test for application
    • src/*: contains code for the service

Example for products service

products
├── __tests__ <-- teet folder
│   └── productHandler.test.js
├── prisma
├── src
│   └── handler.js <-- main handler file
├── .env <-- dot env file
├── .env.dev <-- dot env file
├── package.json
└── serverless.yaml <-- define service resources

Microservices workflows

  • CRUD product
  • Direct HTTP: from products service, when search products, the search term is logged by calling http api of activity_logs service with payload is search term. This is for demonstration purpose only. We shouldn't do this in production.
  • SQS workflow: when filter products by color or brand, the filters are sent as a SQS message to a ActivityLogsQueue, the activity_logs service process messages from the queue and process it (saving into database)
  • Kinesis workflow: when view a product, the a record is put to kinese ActivityLogsStream, activity_logs service poll records and process them (saving into database)

Database ER diagram

ER diagram

Architecture diagram

Architecture diagram

TODOs

  • Products CRUD
    • Create
    • Update
    • Delete
    • Get
    • List
    • List with custom pagination
  • Seed data
    • Products
    • Brands
  • Product search
    • By name
  • Product filter
    • By color
    • Brand
  • Streaming and store data for analytics.
    • Store search products logs - use direct http call
    • Store view product logs - use SQS
    • Stream view products - use Kinesis
  • Other
    • Draw architecture diagram
    • Draw ER model
    • Explanation for code structure
    • Steps to run the application
    • List curl commands to test apis

Steps to run application

  • global requirements
    • node version v14.14.0 or above
    • Docker
    • npm install -g serverless@latest
  • from root folder
    • docker-compose up
  • from products folder
    • npm install
    • npm start
    • npm test (optional)
    • if there is a problem starting the service, try running npm run db:reset
  • from activity_logs folder
    • npm install
    • serverless dynamodb install
    • serverless dynamodb start
    • npm start
    • npm test (optional)

Demo video: https://www.loom.com/share/c7ae0540a17248cfa0ddcb9fb3d71a42

API endpoints

Entity Endpoint Method Description
Product http://localhost:3000/dev/products POST Create a new product
Product http://localhost:3000/dev/products/{id} GET Get a product
Product http://localhost:3000/dev/products/{id} DELETE Detete a product
Product http://localhost:3000/dev/products/{id} PUT Update a product
Product http://localhost:3000/dev/products GET List products
Product http://localhost:3000/dev/products?search=productName GET Search products
Product http://localhost:3000/dev/products?page=2&pageSize=10 GET Custom pagination
Product http://localhost:3000/dev/products?color=red,blue GET Filter by colors
Product http://localhost:3000/dev/products?brand=dior GET Filter by brand
Activity Logs http://localhost:3000/activityLogs GET List activity logs
Activity Logs http://localhost:3000/activityLogs POST Create activity logs

Curl commands to test apis

Located in curl-command.md file

Commands

  • reset: npx prisma migrate reset --preview-feature

  • migrate: npx prisma migrate dev --preview-feature

  • seed: npx prisma db seed --preview-feature

  • generate: npx prisma generate

  • send a sqs message: aws sqs --endpoint-url http://localhost:9324 send-message --queue-url http://localhost:9324/queue/ActivityLogsQueue --message-body "MyFirstMessage"

  • put a kinesis record, data must be base64: aws kinesis --endpoint-url http://localhost:4567 put-record --stream-name ActivityLogsStream --partition-key "view product" --data "TXlGaXJzdE1lc3NhZ2UK"

  • sls dynamodb install

  • sls dynamodb start

About


Languages

Language:JavaScript 92.0%Language:Shell 8.0%