Install pip in Serverless Framework and AWS Lambda with Python environment

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.

Procedure outline

You can easily do it by inserting a plugin.

  1. serverless plugin install -n serverless-python-requirements
  2. Create requirements.txt
  3. After that, if you deploy normally, it will do various things without permission

Detailed procedure

Serverless Framework service creation

$ 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

Plugin installation

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.

Source code

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.

Python source code

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

Deploy

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

Run

If you look at Lambda in the AWS Management Console after deployment, it looks like this:

image.png

Unlike Ruby, it seems that the library is stored directly in Lambda instead of Layer.

Link

-(Ruby version) Gem installation in Serverless Framework and AWS Lambda with Ruby environment

Recommended Posts

Install pip in Serverless Framework and AWS Lambda with Python environment
Touch AWS with Serverless Framework and Python
Deploy Python3 function with Serverless Framework on AWS Lambda
Install Python 2.7.9 and Python 3.4.x with pip.
Install CaboCha in Ubuntu environment and call it with Python.
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Dynamic HTML pages made with AWS Lambda and Python
[Python] Scraping in AWS Lambda
Install Python environment with Anaconda
Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
Pip install in proxy environment
Start numerical calculation in Python (with Homebrew and pip)
Until you install TensorFlow-GPU with pip in Windows environment
Make ordinary tweets fleet-like with AWS Lambda and Python
Install Jupiter Notebook on Windows with pip in proxy environment
Site monitoring and alert notification with AWS Lambda + Python + Slack
Install scrapy in python anaconda environment
Write AWS Lambda function in Python
install tensorflow in anaconda + python3.5 environment
Install Python framework django using pip
Notify HipChat with AWS Lambda (Python)
[Python Windows] pip install with Python version
Build a local development environment for Lambda + Python using Serverless Framework
How to use Serverless Framework & Python environment variables and manage stages
Prepare an environment to use OpenCV and Pillow with AWS Lambda
[AWS] Using ini files with Lambda [Python]
Install Python as a Framework with pyenv
Python installation and package management with pip
scipy stumbles with pip install on python 2.7.8
Dealing with "years and months" in Python
Connect to s3 with AWS Lambda Python
Clean python environment with pythonz and virtualenv
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Python + Selenium + Headless Chromium with aws lambda
Let's install box2d-py with Windows 10 environment pip
(For myself) Flask_AWS_1 (Install PHP, MySQL, phpMyAdmin, Python in AWS virtual environment)
How about creating a virtual environment with Anaconda and doing pip install?
What to do if you can't install with pip in babun environment
[AWS] Problem avoidance memo when installing awscli and boto3 in Python 2.7 environment
Upload what you got in request to S3 with AWS Lambda Python
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Use additional Python packages with Serverless Framework (v1.x)
Regular serverless scraping with AWS lambda + scrapy Part 1.8
Building a python environment with virtualenv and direnv
Install python package in personal environment on Ubuntu
Create a virtual environment with conda in Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Install pip and pandas with Ubuntu or VScode
LINE BOT with Python + AWS Lambda + API Gateway
Install the python package in an offline environment
Install Python 3.5.1 + numpy + scipy + α in Windows environment
Install gensim in conda environment (and also install mecab)
Work in a virtual environment with Python virtualenv.
Use Python in Anaconda environment with VS Code
Getting started with AWS IoT easily in Python
Amazon API Gateway and AWS Lambda Python version
Build PyPy and Python execution environment with Docker
Install ZIP version Python and pip on Windows 10