项目作者: anuradhawick

项目描述 :
Boilerplate for AWS lambda deployment using serverless framework
高级语言: JavaScript
项目地址: git://github.com/anuradhawick/aws-lambda-serverless-boilerplate.git


aws-lambda-serverless-boilerplate

An easy to use scalable boilerplate for AWS serverless deployment. Contains key artifacts;

  1. Router
  2. Multiple service handling
  3. Database connector
  4. Offline Runner

Read the blog in Medium

Adding services

  • Create a folder for service
  • Create a service.yml inside it
  • Make necessary modifications like below
    ```yaml

    packages to include

    package:

    exclude all folders and files

    exclude:
    • */

      include all that is relevant to the service

      include:
    • node_modules/**
    • utils/**
    • second-service/**

service: second-service

functions:
second-service:
handler: second-service/second-main.main
name: ${self:service}-${opt:stage}
environment:
MONGODB_ATLAS_CLUSTER_URI: ${self:custom.env.MONGODB_ATLAS_CLUSTER_URI}
user_pool_id: ${self:custom.config.user_pool_id}
BUCKET_NAME: ${self:custom.env.BUCKET_NAME}
BUCKET_REGION: ${self:custom.env.BUCKET_REGION}
events:

  1. - http:
  2. # implementing GET request to the end point /users of second service.
  3. # note that we do not have base path PATH1 here since it is not requires as we
  4. # are already in service of PATH1
  5. method: get
  6. path: users
  7. cors: true
  8. # Example authorization with a user pool
  9. authorizer:
  10. name: vinyl-authorizer
  11. arn: ${self:custom.config.userpool_authorizer_arn}
  1. * Deploy using command
  2. `serverless deploy --stage STAGE --service SERVICE_FOLDER`
  3. ## Using Router
  4. ```js
  5. // create router instance
  6. const router = new lambdaRouter.Router(event, context, callback);
  7. // calling route method
  8. // acts as a switch case, pass method, path template string and a handler
  9. // call this router.route function as many times as you like with your methods and paths
  10. // for readability you could implement the function logic in a separate functions file as I have done
  11. router.route(
  12. 'GET',
  13. '/data',
  14. (event, context, callback) => {
  15. first_functions.data(event.queryStringParameters).then((data) => {
  16. callback(null, lambdaRouter.builResponse(200, {
  17. ...data,
  18. success: true
  19. }))
  20. }).catch((e) => {
  21. console.error(e);
  22. callback(null, lambdaRouter.builResponse(
  23. 500,
  24. {
  25. records: "ERROR",
  26. success: false
  27. })
  28. );
  29. }
  30. );
  31. }

Using DB Connection

  • The DB hanlder supports MongoDB
  • Simply import the DB hanlder and call connect function.
  1. const db_util = require("../utils/db-util");
  2. const db = await db_util.connect_db(); // returns a promise

Testing offline

  • Once a new service is registerd modify the offline-serverless.js files services array as follows.
  1. const services = [
  2. {route:/^\/PATH1/, path:'first-service', port:3001},
  3. {route:/^\/PATH2/, path:'second-service', port:3002},
  4. {route:/^\/NEW_PATH/, path:'new-service', port:3003} // <- New service, new port
  5. ];
  • Use command

node offline-serverless.js