Create Python version Lambda function (+ Lambda Layer) with Serverless Framework

This is a record of settings when a Lambda function written in Python language is created and deployed using Serverless Framework.

environment

Project setup

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.

Initial deployment

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.

Function check

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"
    }
}

Introduce an external library using Pipenv

This is the setting when using an external Python library. This time, we will use Pipenv as the package manager.

Pipenv setup

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"

Install Python external library

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.

Introducing serverless-python-requirements

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

Deployment, operation check

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"
    }
}

Use Lambda Layer

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.

Summary

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.

Reference material

-Summary of how to use Serverless Framework -Beginning with Pipenv -Python date and time processing -Speed up deployment with Lambda Layer

Recommended Posts

Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
Deploy Python3 function with Serverless Framework on AWS Lambda
Create a Layer for AWS Lambda Python with Docker
Create a Python function decorator with Class
Install pip in Serverless Framework and AWS Lambda with Python environment
Touch AWS with Serverless Framework and Python
Play with Lambda layer (python) for about 5 minutes
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Check version with python
Create a simple reception system with the Python serverless framework Chalice and Twilio
Operate TwitterBot with Lambda, Python
Easily serverless with Python with chalice
Create a function in Python
Create 3d gif with python3
Deploy Django serverless with Lambda
Specify python version with virtualenv
Create a directory with python
Effortlessly with Serverless Python Requirements
Create a Mastodon bot with a function to automatically reply with Python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Create another version of Python conda environment with one command line
Create plot animation with Python + Matplotlib
[AWS] Create API with API Gateway + Lambda
Build a local development environment for Lambda + Python using Serverless Framework
Create folders from '01' to '12' with python
Face detection with Lambda (Python) + Rekognition
Write AWS Lambda function in Python
Create a virtual environment with Python!
Create an Excel file with Python3
[Python] Make the function a lambda function
Create a Python general-purpose decorator framework
Manage each Python version with Homebrew
Notify HipChat with AWS Lambda (Python)
Use PostgreSQL with Lambda (Python + psycopg2)
[For Python] Quickly create an upload file to AWS Lambda Layer
[Python Windows] pip install with Python version
How to create a serverless machine learning API with AWS Lambda
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Let's create a chat function with Vue.js + AWS Lambda + dynamo DB [AWS settings]
[AWS] Using ini files with Lambda [Python]
Create RESTful APIs with Django Rest Framework
Automatically create Python API documentation with Sphinx
Create wordcloud from your tweet with python3
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Create Python + uWSGI + Nginx environment with Docker
Manipulate DynamoDB data with Lambda (Node & Python)
Create and decrypt Caesar cipher with python
Create a virtual environment with Python_Mac version
Learn Python! Comparison with Java (basic function)
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Lambda function to take AMI backup (python)
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
[Python] Quickly create an API with Flask
Connect to s3 with AWS Lambda Python