项目作者: msfidelis

项目描述 :
:package: :zap: :rocket: Boilerplate to organize and deploy big projects using AWS API Gateway and AWS Lambda with Serverless Framework
高级语言: JavaScript
项目地址: git://github.com/msfidelis/serverless-architecture-boilerplate.git
创建时间: 2017-10-23T21:48:19Z
项目社区:https://github.com/msfidelis/serverless-architecture-boilerplate

开源协议:MIT License

下载


Logo

Serverless Architecture Boilerplate serverless License: MIT Build Status

CI / CD Pipeline

Need a Codepipeline Structure to deploy your Serverless Project with Quality? See this repo!

Serverless Archictecture Boilerplate with Go?

Looking for boilerplates to organize big projects using AWS Lambda with Go? See this repo

Structure

  1. .
  2. ├── modules (modules folder)
  3. └── books (module / context)
  4. ├── endpoints (API endpoints)
  5. ├── create.js
  6. ├── delete.js
  7. ├── read.js
  8. └── update.js
  9. └── functions (workers / background functions)
  10. └── worker
  11. └── handler.js
  12. ├── package.json
  13. ├── serverless.yml (serverless config)
  14. ├── handlers (functions config)
  15. ├── books-endpoints.yml (endpoints config)
  16. └── books-workers.yml (workers config)
  17. ├── shared (shared components)
  18. └── lib (shared libraries)
  19. ├── dynamo.js
  20. ├── kinesis.js
  21. ├── lambda.js
  22. ├── parsers.js
  23. ├── sqs.js
  24. └── uuid.js
  25. └── test (tests folder)
  26. └── unit (unit tests folder)
  27. ├── modules (unit tests for modules)
  28. └── books
  29. └── shared (unit tests for shared components)
  30. └── lib (unit tests for libraries)
  31. ├── dynamo.test.js
  32. ├── kinesis.test.js
  33. ├── parsers.test.js
  34. ├── sqs.test.js
  35. └── uuid.test.js

Functions

HTTP Trigger Function (API Gateway)

  1. functions:
  2. # API Endpoints
  3. books-register:
  4. handler: modules/books/endpoints/create.create #Path to function
  5. memorySize: 128 # Lambda Memory Limit
  6. timeout: 60 # Lambda Timeout
  7. events:
  8. - http: # HTTP Trigger
  9. path: services/books # API Endpoint
  10. method: post # HTTP Method

Cloudwatch Events Functions (Cron)

Lambda Schedule Docs

  1. # Background Function
  2. books-consumer:
  3. handler: modules/books/functions/worker/handler.worker #Path to function
  4. events:
  5. - schedule: #Cloudwatch Event Trigger
  6. rate: cron(* * * * * *) # Cron Syntax
  7. enabled: true # Trigger Enabled

Development environment

This boilerplate uses serverless-local plugin and some containers and plugins to emulate the AWS Resources

  1. docker-compose up

The applications will start on http://localhost:3000

Dev Plugins

This boilerplate contains following plugins for local development:

Production environment

Deploy full services

  1. serverless deploy -v

asciicast

Deploy a function

  1. serverless deploy function -f books-consumer

Get function logs

  1. serverless books-consumer -f bananinha -t

Clean All

  1. serverless remove

Testing

Create Book

  1. curl -X POST \
  2. -H "Content-Type: application/json" \
  3. -d '{"title": "American Gods", "author": "Neil Gaiman", "price": 10.00 }' \
  4. https://yur25zhqo0.execute-api.us-east-1.amazonaws.com/production/services/books -i

List Books

  1. curl -X GET \
  2. https://yur25zhqo0.execute-api.us-east-1.amazonaws.com/production/services/books

asciicast

Detail Book

  1. curl -X GET \
  2. https://yur25zhqo0.execute-api.us-east-1.amazonaws.com/production/services/books/456c9e8f-6c50-d656-dc69-dc828c42af65

Delete Book

  1. curl -X DELETE \
  2. https://yur25zhqo0.execute-api.us-east-1.amazonaws.com/production/services/books/456c9e8f-6c50-d656-dc69-dc828c42af65 -i

Update Book

  1. curl -X PUT \
  2. -d '{"title": "updated modafoca"}' -H "Content-type: application/json" \
  3. https://eusrv4mci5.execute-api.us-east-1.amazonaws.com/production/services/books/bbafdb0c-ee6e-fca0-f224-ed534f5b7766 -i

asciicast

Custom and Environment Variables

Custom Items

Creating and Using custom variables to build dynamic name

  1. custom:
  2. region: ${self:provider.region}
  3. stage: ${opt:stage, self:provider.stage}
  4. prefix: ${self:custom.stage}-${self:service}
  5. process: ${self:custom.prefix}-process
  6. config: ${self:custom.prefix}-config
  7. dynamo-books: ${self:custom.prefix}-BooksCatalog
  8. sns-logs: ${self:custom.prefix}-trigger-logs
  9. sqs-logs: ${self:custom.prefix}-messages-logs

Environment Variables

Building URL Resources using CloudFormation parameters and Custom Variables

  1. environment: # Global Environment variables
  2. DYNAMO_TABLE_BOOKS: ${self:custom.dynamo-books} # Reference to Custom Env
  3. SQS_QUEUE_URL: 'https://sqs.${self:provider.region}.amazonaws.com/#{AWS::AccountId}/${self:custom.sqs-logs}'
  4. REGION: ${self:custom.region}

Manage AWS Cloudformation with Serverless

IAM Roles

IAM Docs

  1. iamRoleStatements: # Permissions for all of your functions can be set here
  2. - Effect: Allow
  3. Action: # Gives permission to DynamoDB tables in a specific region
  4. - dynamodb:DescribeTable
  5. - dynamodb:Query
  6. - dynamodb:Scan
  7. - dynamodb:GetItem
  8. - dynamodb:PutItem
  9. - dynamodb:UpdateItem
  10. - dynamodb:DeleteItem
  11. Resource: "arn:aws:dynamodb:us-east-1:*:*"
  12. - Effect: Allow
  13. Action: # Gives permission to Lambda execution
  14. - lambda:InvokeFunction
  15. - lambda:InvokeAsync
  16. Resource: "*"

Manage Infrastructure Components - Docs

  1. # Infrastrucure - Cloud Formation
  2. resources: # CloudFormation template syntax
  3. Resources:
  4. #DynamoDB Books Table
  5. BooksCatalog:
  6. Type: AWS::DynamoDB::Table # CloudFormation Pseudo Parameter Example
  7. Properties:
  8. TableName: ${self:custom.dynamo-books}
  9. AttributeDefinitions:
  10. - AttributeName: hashkey
  11. AttributeType: S
  12. KeySchema:
  13. - AttributeName: hashkey
  14. KeyType: HASH
  15. ProvisionedThroughput:
  16. ReadCapacityUnits: 2
  17. WriteCapacityUnits: 1
  18. # SQS Queue to Update DynamoDB
  19. BooksQueueExample:
  20. Type: AWS::SQS::Queue
  21. Properties:
  22. QueueName: ${self:custom.sqs-logs}
  23. MessageRetentionPeriod: 1209600
  24. VisibilityTimeout: 60