Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Documentation for Creating, Managing and Deploying Lambda Functions with Serverless:
Steps to Deploy a Flask Application with Circle CI:
1. Run command: npm install -g serverless
2. serverless create --template aws-python3 --path 'name-of-project'
3. cd ‘name-of-project’
4. '' will be created, and should be edited to have the following format:
from flask import Flask
app = Flask(__name__)
def handler():
//your python function code here
NOTE: The route you define in your Flask application will be the route on API Gateway
4. 'serverless.yml' will be created, and should be edited to have the following format:
service: your-service-name
name: aws
runtime: python3.6
role: your-lambda-arn-value
region: ap-northeast-1
- serverless-wsgi
handler: wsgi.handler
events:(these next 2 lines MUST be included for a Serverless app with Flask)
- http: ANY /
- http: ANY {proxy+}
4. Connect local directory to github repository
5. Connect github repository to Circle CI
6. Create requirements.txt with 'Flask==0.12.2' and other libraries needed ONLY for AWS lambda (example: AWS Lamba does not need 'boto3' to be in requirements.txt)
7. Add circle.yml file with the following structure:
version: 3.6.2
version: 8.6.0
- pip install -r requirements.txt
NOTE: the libraries below are not included in requirements.txt because they are needed only for Circle CI container,
not to be packaged for AWS Lambda. It is possible that if too many libraries are included in requirements.txt file,
the Serverless app will become too large to deploy to AWS.
- pip install virtualenv
- pip install boto3
- pip install -U pytest
- npm install -g serverless
- npm install --save serverless-wsgi
if you have any tests, run them here
You will need to setup your AWS credentials here. Then, to run the app: 'serverless deploy -v'
8. Now when committing to your github repository, your lambda code will be built, tested, and deployed!
Steps to Deploy Node.js lambda:
1. Use ‘aws configure ‘ to set key and token values
2. serverless create --template aws-nodejs --path 'name of project'
3. cd ‘name of project’
4. serverless deploy -v
Steps to Deploy Python 3 lambda (with 3rd party libraries)
1. Use ‘aws configure’ to set key and token values
2. serverless create —template aws-python3 --path 'name-of-project'
3. cd ‘name of project’
4. virtualenv -p python3 'name-of-virtual-environment'
5. . 'name-of-virtual-environment'/bin/activate
6. pip install 'name-of-package'
7. pip freeze > requirements.txt
8. npm init (you can keep pressing 'enter' until the process finishes)
9. npm install --save serverless-python-requirements
10. Edit 'serverless.yml' file with the line:
- serverless-python-requirements
10. serverless deploy -v
When adding a new Python library:
1. pip freeze > requirements.txt
2. serverless deploy -v
Steps to deploy function only:
serverless deploy function --function 'name-of-function'
Testing lambda:
serverless invoke --function 'name-of-function' --data 'file-name'
Testing lambda locally:
serverless invoke -local --function 'name-of-function' --data 'file-name'
--> only works with Node.Js, Python & Java runtimes
--> data can be formatted in json
Adding AWS Resources (called events with Serverless):
Edit the serverless.yml file under:
Below the 'events' keyword, you can add resources such as S3, SNS, API Gateway, etc
- sns: greetings
- http: (This refers to an API Gateway)
path: user/profile
method: POST
Adding Environment Variables:
Edit the serverless.yml file under:
variable2: value2
Then, run:
serverless deploy -v
Injecting secrets:
1. Run command: npm install --save-dev serverless-kms-secrets
2. add to serverless.yml:
- serverless-kms-secrets
3. Add this block of code below 'plugins':
secretsFile: kms-secrets.${opt:stage, self:provider.stage}.${opt:region, self:provider.region}.yml
4. Run command: serverless encrypt -n VARIABLE_NAME -v 'value-to-encrypt' -k keyId
5. Output will now be in a yml file
6. Add the key to the 'environment' section of the yml file:
MY_VARIABLE: ${file(kms-secrets.${opt:stage, self:provider.stage}.${opt:region, self:provider.region}.yml):secrets.VARIABLE_NAME}
7. Run command: serverless deploy -v
Adding more secrets:
1. Run command: serverless encrypt -n VARIABLE_NAME -v 'value-to-encrypt' -k keyId
2. Output will now be in the same yml file
3. Add new secret variable to serverless.yml file
4. Run command: serverless deploy -v
Testing API Gateway Locally (only with Node.js):
1. Run command: npm install serverless-offline --save-dev
2. Add the following to your yaml file:
- serverless-offline
3. Run command: serverless offline start
Now the lambda function will be invoked when send a request to API gateway
Deleting Lambda Function and Infrastructure:
1. serverless remove
About Serverless Frameworks:
- The most comprehensive deployment tool for AWS Lambda
- Detailed documentation for setup, configuration, and 3rd party plugins
- Allows for easy management of infrastructure
- Allows for API keys to be specified
- Can specify domains with Route53
- Many 3rd party libraries to extend functionality
- Functions can be tested locally
- Can inject secrets into lambda functions
- Also supports google and microsoft functions
- Development is growing fast
- Setting up Python functions with 3rd party libraries is not very simple
- Can test API gateway locally, but only with Node.js functions
- Not possible to manage SSL certificates from Serverless
- No tool that provides minimum permissions needed
- Deploy process can be slow
Other Serverless Deployment Frameworks:
- Only for python applications
- Automatically detects frameworks like Flask and Django.
- A much simpler process adding 3rd party libraries to the code as Zappa relies on virtualenv
- This framework is primarily intended for Python lambda functions with API Gateway, but
it is possible to add different triggers such as SNS.
- This framework is probably be a better option if only working with Python
- Deploy process much faster than Serverless
- Very easy commands and setup
- Only for Node.js
- A very young framework
- Meant for deploying and hosting web applications
- Not specific to AWS Lambda, uses a combination of different serverless platforms
- Easy to create a domain name and SSL certificate to be used
- Documentation is short and not detailed
- Difficult to manage infrastructure
- Deploy process much faster than Serverless
AWS CodePipeline:
- A continuous delivery service
- Builds, tests, and deploys an application
- Can run load tests on an application
- Deployed to EC2 using AWS CodeDeploy, AWS Elastic Beanstalk, or AWS OpsWorks Stack
- It does integrate with Lambda functions, but it is not intended to be a serverless framework
- Requires lengthy setup and configuration
Conclusion: Serverless provides the most complete functions. However, there is still no perfect, 100% stable solution
for managing AWS Lambda functions. Perhaps this is because the serverless concept is still new, and it will take a long
time to move from server-based applications. AWS Lambda in particular seems oriented towards Node.js applications and
even AWS does not have a complete solution for managing Lambda functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment