Jevy Wang's Blog

Package AWS Lambda function

  • Install/start Docker
  • Install python3
  • Install nodejs (npm included)
  • Install serverless: npm install -g serverless
  • Install virtualenv of python pip install virtualenv

Create Serverless project

1
serverless create   --template aws-python3  --name project-name  --path project-path

Active 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
2
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-reagon
role: aws-iam-role-with-the-privilidge-to-create-lambda-function
plugins:
- serverless-python-requirements
# you can add packaging information here
package:
exclude:
- vent/**

functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
cors: true
integration: LAMBDA
- schedule: rate(1 hour)

Deploy to AWS:

1
serverless deploy

Useful tools:

  • serverless logs –function hello #check last log
  • serverless invoke -f hello –log #invoke the function

专题:

本文发表于 2020-03-12,最后修改于 2020-03-13。

本站永久域名jevy.wang,也可搜索「 Jevy Wang's Blog 」找到我。

期待关注我的 知乎专栏 ,查看最近的文章和动态。


上一篇 « Read H264 livestream using ffmpeg and cv2 下一篇 » Install owncloud with docker and local directory

赞赏支持

请我吃胡萝卜 =^_^=

i ali

支付宝

i wechat

微信

推荐阅读

Big Image