--This article is Python Part 3 Advent Calendar 2019 is the 16th day. --This is a memo until you deploy and publish a Python3 function with Serverless Framework on AWS Lambda.
--The npm command can be used --There is an IAM user for serverless framework --You can refer to the official this area (Administrator Access is recommended). --Docker can be used
Installation is one shot of npm install.
$ npm install serverless
Make sure the path is in --version.
$ serverless --version
Framework Core: 1.59.3
Plugin: 3.2.5
SDK: 2.2.1
Components Core: 1.1.2
Components CLI: 1.4.0
Set the IAM account for Serverless Framewrok created in advance with serverless config credentials.
serverless config credentials --provider aws --key foo --secret bar
This completes the preparations.
Generate a template project for Python3.
serverless create --template aws-python3 --name aws-lambda-hello-python --path aws-lambda-hello-python
The following files are actually generated.
$ cd aws-lambda-hello-python
$ tree
.
├── handler.py
└── serverless.yml
0 directories, 2 files
In the above command, the main method is not generated, but this time I wanted to make it work locally, so I added it.
handler.py
import json
def hello(event, context):
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
# Use this code if you don't use the http event with the LAMBDA-PROXY
# integration
"""
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event
}
"""
if __name__ == "__main__":
print(json.dumps(hello("","")))
There are a lot of comments, but the minimum required is: (Since you haven't specified a region, it will be deployed to the default us-east-1)
serverless.yml
service: aws-lambda-hello-python
provider:
name: aws
runtime: python3.8
functions:
hello:
handler: handler.hello
Let's move it a little with Local.
$ pyenv local 3.8.0
$ python3 handler.py
{"statusCode": 200, "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": \"\"}"}
It worked. Now, let's deploy and check the operation.
$ serverless deploy -v
(Message omitted)
$ serverless invoke -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
It seems that it was able to deploy properly.
--I would like to be able to use an external library. ――I would like to introduce numpy, but the native library needs to be built for each environment, and it is a little troublesome when using it with Lambda normally, but using Serverless Framework makes it relatively easy.
Make the venv environment available, install numpy and freeze it.
$ python3 -m venv .venv
$ .venv/bin/pip install numpy
$ .venv/bin/pip freeze > requirements.txt
Add the process that uses numpy appropriately. (The mean will change).
handler.py
import json
import numpy as np
def hello(event, context):
arr = range(1, 100+1)
mean = np.mean(arr)
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event,
"mean" : mean
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
# Use this code if you don't use the http event with the LAMBDA-PROXY
# integration
"""
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event
}
"""
if __name__ == "__main__":
print(json.dumps(hello("","")))
When executed locally, it looks like this.
$ .venv/bin/python3 handler.py
{"statusCode": 200, "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": \"\", \"mean\": 50.5}"}
Install a plugin that allows serverless to use requirements.
npm install serverless-python-requirements
Add to serverless.yml (add plugins and below)
service: aws-lambda-hello-python
provider:
name: aws
runtime: python3.8
functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
package:
include:
- handler.py
exclude:
- '**'
Try deploying (docker will run a numpy build and deploy it)
$ serverless deploy -v
Let's check the operation.
$ serverless invoke -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}, \"mean\": 50.5}"
}
Numpy is working properly! Let's have a fun Python & Serverless life!
Recommended Posts