How to deploy AWS Lambda Python scripts that require pip installation with Serverless Framework.
I wrote about the AWS Lambda Ruby script that requires gem installation in the Last time article.
You can easily do it by inserting a plugin.
serverless plugin install -n serverless-python-requirements
requirements.txt
$ serverless create --template aws-python3
Serverless: Generating boilerplate...
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v2.16.1
-------'
Serverless: Successfully generated boilerplate for template: "aws-python3"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name
Three files will be generated.
.gitignore
handler.py
serverless.yml
Install the plugin serverless-python-requirements
.
$ serverless plugin install -n serverless-python-requirements
The following files and directories will increase.
node_modules
package.json
package-lock.json
Since the Serverless Framework is implemented in Node.js, it seems that node_modules
and package.json
exist even though it is a Python project.
serverless.yml
serverless.yml
has the following contents. The description of plugins
is added without permission when the plugin is installed.
serverless.yml
service: sample
frameworkVersion: '2'
provider:
name: aws
runtime: python3.8
region: ap-northeast-1
functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
requirements.txt
Let's use a library called jpholiday
as a sample library. A library for determining Japanese holidays.
Create requirements.txt
and make it as follows. This is a file with only one line of library name.
jpholiday
Let's use a library called jpholiday
as a sample. A library for determining Japanese holidays.
handler.py
import datetime
import jpholiday
def hello(event, context):
holidayName = jpholiday.is_holiday_name(datetime.date(2021, 8, 8))
print(holidayName) #To CloudWatch"Mountain day"Is written out
If you create it so far and then deploy it with the serverless
command, not only Lambda itself but also the serverless
command will automatically create an image with the library installed and upload it as a layer of AWS Lambda.
$ serverless deploy -v
If you look at Lambda in the AWS Management Console after deployment, it looks like this:
Unlike Ruby, it seems that the library is stored directly in Lambda instead of Layer.
-(Ruby version) Gem installation in Serverless Framework and AWS Lambda with Ruby environment
Recommended Posts