Package AWS Lambda
- Install/start Docker
- Install python3
- Install nodejs (npm included)
- Install serverless: npm install -g serverless
- Install virtualenv for Python:
pip install virtualenv
Create Serverless project
1
serverless create --template aws-python3 --name project-name --path project-path
Activate virtualenv at project directory:
1
2
3
4
5
virtualenv venv --python=python3
# On Windows
.\venv\Script\activate
# On Linux
source venv/bin/activate
Install python package and code as usual in python virtual environment
Freeze python package to requirements.txt pip freeze > requirements.txt
Install serverless-python-requirements and config the plugin at serverless.yml
1
npm install --save serverless-python-requirements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
service: project-name # NOTE: update this with your service name
provider:
name: aws
runtime: python3.6
region: aws-region
role: aws-iam-role-with-the-privilege-to-create-lambda-function
plugins:
- serverless-python-requirements
# you can add packaging information here
package:
exclude:
- venv/**
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
cors: true
integration: LAMBDA
- schedule: rate(1 hour)
Deploy to AWS:
1
2
serverless deploy
Useful tools:
- serverless logs –function hello #check last log
- serverless invoke -f hello –log #invoke the function
This post is licensed under CC BY 4.0 by the author.