Boilerplate template for the serverless-framework
MIT License
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
Deploy two serverless services (APIs)
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
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}"
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
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'
yarn add some_package
in an API folder
yarn add --dev some_package
in an API folder; Lambda Layer supplies the "real" dependenciesServerless 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
CloudFormation templates for aws-resources
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:
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push --set-up-stream origin my-new-feature
)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