Boilerplate template for the serverless-framework.
Want to learn how to get start with the serverless framework? Watch my YouTube video where I explain about how to use the framework, while using this GitHub repository.
-
AWS account
Goal
Deploy two serverless services (APIs)
- todo-api - NodeJS 12.x (JavaScript/TypeScript)
- CRUD app - To keep it simple, we're using an S3 bucket as a database. The contents are saved to the objects' user-defined metadata
- greet-api - Python 3.8
- Send a name and get a greeting
Both services have dependencies, and we'll use Lambda Layers to meet these dependencies.
-
Clone this repository (or Use as template and then clone)
$ (home) git clone https://github.com/unfor19/serverless-template.git
-
Use this Docker image
Image Usage Examples
-
With aws-vault
$ (serverless-template) aws-vault exec PROFILE_NAME -- bash ./scripts/docker_run.sh
-
AWS Environment variables
$ (serverless-template) export AWS_SECRET_ACCESS_KEY=A123123 $ (serverless-template) export AWS_ACCESS_KEY_ID=B1232123123 $ (serverless-template) export AWS_REGION=eu-west-1 $ (serverless-template) export AWS_PROFILE=my-profile-name $ (serverless-template) bash ./scripts/docker_run.sh
-
AWS Credentials & Config files
$ (serverless-template) bash ./scripts/docker_run.sh
Tip: Take a look at the docker_run script
$ (serverless-template) bash ./scripts/docker_run.sh ... # Pulling image ... $ /code (master) # We're in!
-
-
Build App - this includes installing dependencies
$ /code (master) bash ./scripts/app_build.sh 🔎 Identifying services folders ... ... ✅ Finished
-
Deploy AWS resources - S3 Bucket and API Gateway
$ /code/aws-resources (master) yarn deploy:dev
-
Deploy AWS Lambda Layers
$ /code/services/todo-api/layer (master) yarn deploy:dev $ /code/services/greet-api/layer (master) yarn deploy:dev
-
Deploy AWS Lambda Functions
$ /code/services/todo-api (master) yarn deploy:dev $ /code/services/greet-api (master) yarn deploy:dev
Replace ENDPOINT
with the API Gateway's endpoint that was generated by serverless-framework, and AWS_REGION
with the relevant region.
$ /code (master) APIGATEWAY_ENDPOINT=https://ENDPOINT.execute-api.AWS_REGION.amazonaws.com
Replace MY_CONTENT
MY_CONTENT="some content"
curl --location --request POST ${APIGATEWAY_ENDPOINT}/dev/todo/create \
--header 'Content-Type: application/json' \
--data-raw '{ "content": "'"${MY_CONTENT}"'" }'
Replace MY_UUID
MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'
curl --location --request GET ${APIGATEWAY_ENDPOINT}/dev/todo/get/${MY_UUID}
Replace MY_UUID
and MY_CONTENT
MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'
MY_CONTENT='wohoo new content!'
curl --location --request POST ${APIGATEWAY_ENDPOINT}/dev/todo/update \
--header 'Content-Type: application/json' \
--data-raw ' { "id": "'${MY_UUID}'", "content": "'"${MY_CONTENT}"'" }'
Replace MY_UUID
MY_UUID='cf27a7de-f3f7-43a0-b12f-ff8016b7b7e0'
curl --location --request DELETE ${APIGATEWAY_ENDPOINT}/dev/todo/delete/${MY_UUID}
curl --location --request GET ${APIGATEWAY_ENDPOINT}/dev/todo/list
Replace MY_NAME
MY_NAME="Willy"
curl --location --request GET "${APIGATEWAY_ENDPOINT}/dev/greet/${MY_NAME}"
Expand/Collapse
-
Clone this repository
-
Use this Docker image
-
Install dependencies for each API
$ /code/services/todo-api (master) yarn install $ /code/services/greet-api (master) yarn install
-
Modify code in
src
and then build$ /code/services/todo-api (master) yarn build:dev $ /code/services/greet-api (master) yarn build:dev
NodeJS - yarn add package_name
$ /code/services/todo-api/layer/nodejs (master) yarn add uuid # or any other package
Python - update the requirements.txt file
$ /code (master) cat ./services/greet-api/layer/python/requirements.txt
greetings==0.1.0
$ /code/services/todo-api/layer (master) yarn deploy:dev
$ /code/services/greet-api/layer (master) yarn deploy:dev
When updating a Lambda Layer, you must re-deploy the API for it to use the latest Lambda Layer version.
$ /code/services/todo-api (master) yarn deploy:dev
$ /code/services/greet-api (master) yarn deploy:dev
-
Destroy AWS Lambda Functions
$ /code/services/todo-api (master) yarn destroy:dev $ /code/services/greet-api (master) yarn destroy:dev
-
Destroy AWS Lambda Layers
$ /code/services/todo-api/layer (master) yarn destroy:dev $ /code/services/greet-api/layer (master) yarn destroy:dev
-
Destroy S3 Bucket and API Gateway
IMPORTANT - remove all the objects from the
todo
S3 bucket before taking this action$ /code/aws-resources (master) yarn destroy:dev
Expand/Collapse
Learn how to use the Serverless Framework, while taking advantage of AWS Lambda Function, Lambda Layer, and API Gateway.
"AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume..." [Source]
"...A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package..." [Source]
"...API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, CORS support, authorization, and access control, throttling, monitoring, and API version management..." [Source]
"The Serverless Framework helps you build serverless apps with radically less overhead and cost. It provides a powerful, unified experience to develop, deploy, test, secure and monitors your serverless applications..." [Source]
Eventually, the serverless framework produces CloudFormation templates, deploys stacks, and manages them.
Tip After deploying with the serverless framework, check the stacks' templates, they look like a total mess. If you want to 'prettify' those YAML templates, click on View in Designer
> Move one of the components, and then look below, your template was automatically 'prettified'
- Each API is an isolated service that contains multiple functions
- All APIs share the same API Gateway - easier to manage
- The file serverless.common.yml contains mappings that are relevant to all APIs, such as region, allow_origin, user_pool_id, and more
- serverless.yml - configuration for deployment - Using Layers
- layer - deployed separately, these are the dependencies
- src - source code of API that is deployed by serverless
- package.json - contains the build, deploy and destroy scripts, and dev-dependencies
- yarn.lock - contains the list of dev-dependencies and their versions
- Never run
yarn add some_package
in an API folder - Always use
yarn add --dev some_package
in an API folder; Lambda Layer supplies the "real" dependencies - There's no need to create a layer for AWS SDK (e.g., aws-sdk, boto3) - These libraries are provided by AWS automatically
- serverless.yml - configuration for deploying the layer - Deploying Layers
- package.json - contains the scripts for building, deploying and destroying the layer
- nodejs/package.json - contains the dependencies that are uploaded with this layer
- nodejs/yarn.lock - contains the list of dependencies and their versions
-
Serverless Best Practices - a free resource to help you build full-stack production-ready Serverless applications
-
AWS Docs - Best Practices for Working with AWS Lambda Functions
-
serverless-template Docker image packages
Expand/Collapse
Package Version awscli 1.18.14 bash 5.0.11 boto3 1.12.16 git 2.24.1 jq 20191114-85-g260888d269 NodeJS 12.16.1 Python 3.8.1 serverless-framework 2.21.1 TypeScript 3.8.2 yarn 1.22.0 -
CloudFormation templates for aws-resources
Expand/Collapse
More regions
To deploy in other regions, replace AWS_REGION with the region's code.
API Gateway
```bash https://AWS_REGION.console.aws.amazon.com/cloudformation/home?region=AWS_REGION#/stacks/quickcreate?templateURL=https:// serverless-template.s3-eu-west-1.amazonaws.com/cfn-apigateway.yml ```
S3 Bucket
```bash https://AWS_REGION.console.aws.amazon.com/cloudformation/home?region=AWS_REGION#/stacks/quickcreate?templateURL=https:// serverless-template.s3-eu-west-1.amazonaws.com/cfn-s3.yml ```
Report issues/questions/feature requests on in the Issues section.
Pull requests are welcome! Ideally, create a feature branch and issue for every single change you make. These are the steps:
- Fork this repo
- Create your feature branch from master (
git checkout -b my-new-feature
) - Commit your remarkable changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push --set-up-stream origin my-new-feature
) - Create a new Pull Request and tell us about your changes
-
Slack - Serverless Contributors - Ask us questions in #help or #general - tag
@Meir Gabay
Created and maintained by Meir Gabay
This project is licensed under the MIT License - see the LICENSE file for details