helloakn / develop-aws-lambda-functions-locally

In this example. I use express node package as the api gateway. separated functions and layers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Develop AWS LAMBDA Functions/Layers as Locally

NodeJs NPM Express Language js-standard-style Star Count

This is just for simple project.
However, you can learn the file structure and strategies for aws lambda to develop in local.

benefit of these file structure is to deploy easily as functions or the whole project.
can be used for :


Table of Contents

  • File Structure
    • Before installation the npm dependences
    • After installation the npm dependences
  • npm Dependences
  • How to Setup
  • How to Run
  • How to Test
  • Swagger
  • Acknowledgement

File Structure

Structure is base on lambda functions.
So we can easily deploy to lambda or ECS or EC2 as the monolith or microservice.
We can separate authorizer and functions too.
All the services on our code would be functions in Lambda and middleware will be lambda Authorizer as well as all the others are the layers.

Before installation the npm dependences

.
├── http                        # dir for sqlite3 database
|   ├── routes                  # sqlite3 db file
│   │   ├── routegenerator.js
│   │   └── routelist.js
|   ├── package.json            # sqlite3 db file
|   └── server.js               # sqlite3 db file
├── lambda                      # nodejs source code
│   ├── functions               # administrator and customer middlewares
│   │   └── ...                 
│   └── layers                  # Controllers , we can deploy them to AWS Lambda functions
│       ├── v1                  # For all admin functions
│       │   ├── package.json
│       │   └── ...
│       └── v2                  # For all user functions
│           ├── package.json
│           └── ...
├── resources                   # for documentations
│   └── ...
├── .eslintrc.cjs               # for standar code check
├── package.json 
├── README.md              
└── ...

After installation the npm dependences

.
├── http                        # dir for sqlite3 database
|   ├── routes                  # sqlite3 db file
│   │   ├── routegenerator.js
│   │   └── routelist.js
|   ├── package.json            # sqlite3 db file
│   ├── node_modules
│   │   └── ...
|   └── server.js               # sqlite3 db file
├── lambda                      # nodejs source code
│   ├── functions               # administrator and customer middlewares
│   │   └── ...                 
│   └── layers                  # Controllers , we can deploy them to AWS Lambda functions
│       ├── v1                  # For all admin functions
│       │   ├── package.json
│       │   ├── node_modules
│       │   │   └── ...
│       │   └── ...
│       └── v2                  # For all user functions
│           ├── package.json
│           ├── node_modules
│           │   └── ...
│           └── ...
├── resources                   # for documentations
│   └── ...
├── .eslintrc.cjs               # for standar code check
├── package.json 
├── node_modules
│   └── ...
├── README.md              
└── ...

npm Dependences

  • Root
    • supervisor ^0.12.0 - to restart app on source code changes or crash app
    • jest ^28.0.3 - for unit testing
    • supertest ^6.2.3 - for api testing
    • http
    • lambda
      • functions
        • nothing to install
      • layers
        • layerauthorizer
        • v1
          • dotenv ^16.0.0 - to load environment variable from .env file or sys environment variable
          • md5 ^2.3.0 - for one way password hex
          • jsonwebtoken ^8.5.1 - JWT
          • mysql ^2.18.1 - for db crud

How to Setup

We have to install npm dependence to run our program smoothly. pls follow as the singlie line of the following command.

npm run setup

alt text

How to Run

Configuration is just for dev(local) only. we can easily execute our program in local as the following command.

npm run dev

How to Test

I use jest and supertest npm package for testing. main program is base on commajs and jest is base on model type. however we can run and test our program because i have configured in package.json. You can check the testing source code in ./test directory too. Here is the command to run the testing.

npm test

alt text

Swagger

we can reach to our swagger from this link...
Link API Doc swagger -> http://localhost:9999/swagger/ We can test the api ourselves.

alt text

Acknowledgement

Thank you all for visiting to my repo. please feel free to contact me.

About

In this example. I use express node package as the api gateway. separated functions and layers.


Languages

Language:JavaScript 100.0%