This is a record of settings when a Lambda function written in Python language is created and deployed using Serverless Framework.
You need to create a Serverless Framework account in advance. https://serverless.com/
Install serverless command
$ npm install -g serverless
Creating a Serverless project
$ serverless
Enter the project name and log in to your account. Choose "AWS Python" as your development language choice.
A new directory for the Serveless project will be created when you complete the entry.
Change to the created directory.
In the initial state, the files serverless.yml
and handler.py
are created.
Make a few changes to each as shown below.
serverless.yml
#Please set service and app according to your environment.
service: python-test
app: python-test
provider:
name: aws
runtime: python3.7
region: ap-northeast-1
functions:
hello:
handler: handler.hello
handler.py
def hello(event, context):
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event
}
It is a simple function that just stores the contents of ʻevent` in the object and returns it.
Deploy (create Lambda function) with the following command
$ serverless deploy
If you look at the Lambda screen from the AWS console, you can see that the new function has been created.
You can execute the function created by the following command.
$ serverless invoke yarn serverless invoke --function hello
{
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": {}
}
You can also pass an event at run time. First, create a ʻevent.json` file with the following contents.
event.json
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Then, when executing the function, specify the file that describes the event with the p option.
$ serverless invoke --function hello -p event.json
{
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
This is the setting when using an external Python library. This time, we will use Pipenv as the package manager.
If you don't have Pipenv installed, use pip to install it.
$ pip3 install pipenv
Initialize the Pipenv settings in the project. Since Python3 system is used this time, specify the version as follows.
$ pipenv --python 3
After executing the command, Pipfile
is created in the directory.
Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "3.7"
This time, I would like to use a library called python-dateutil that enables convenient date processing.
First, install the library with Pipenv.
$ pipenv install python-dateutil
Then change the program to use python-dateutil.
handler.py
import datetime
from dateutil.relativedelta import relativedelta
def hello(event, context):
today = datetime.date.today()
yesterday = today + relativedelta(days=-1)
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event,
"dates": {
"today": f'{today: %Y-%m-%d}',
"yesterday": f'{yesterday: %Y-%m-%d}'
}
}
It is used together with the datetime of the standard library to get the date when the program is executed and the date one day before, and add it in the object to return.
In order to handle the library installed by Pipenv with Serverless Framework, use the npm package called serverless-python-requirements.
serverless-python-requirements originally allows you to handle libraries managed by requirements.txt
, but it also supports Pipenv.
First, use npm to install serverless-python-requirement.
#If you haven't initialized your npm project
$ npm init
# serverless-python-Installation of requirements
$ npm install --save-dev serverless-python-requirements
Next, describe the settings for using serverless-python-requirement in serverless.yml
.
Add the plugin and custom parts.
serverless.yml
service: python-test
app: python-test
provider:
name: aws
runtime: python3.7
region: ap-northeast-1
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
functions:
hello:
handler: handler.hello
Deploy with the serverless deploy
command.
Then let's execute the updated function.
$ serverless invoke --function hello -p event.json
{
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"dates": {
"today": " 2020-03-07",
"yesterday": " 2020-03-06"
}
}
By using Lambda Layer, you don't need to include the library file in the Lambda function package. You can reduce the package capacity.
If you are using serverless-python-requirement, you can easily layer dependent libraries with just a few settings.
Specifically, add the following settings to serverless.yml
.
serverless.yml
custom:
pythonRequirements:
dockerizePip: true
+ layer: true
After changing the settings, deploy with the serverless deploy
command.
From the Lambda screen of the AWS console, check that the new Layers have been created.
I explained how to create a Lambda function in the Python language using the Serverless Framework. By using serverless-python-requirement, you can easily install around external libraries such as integration with Pipenv and creation of Lambda Layer.
-Summary of how to use Serverless Framework -Beginning with Pipenv -Python date and time processing -Speed up deployment with Lambda Layer
Recommended Posts