Create a Layer for AWS Lambda Python with Docker

I wanted to register a module that I often use in the Python runtime of AWS Lambda with Lambda Layers, but I made it possible with Docker because it is troublesome to launch an EC2 instance and create a package.

What are Lambda Layers

--You can upload a zipped file and register it as a "layer". --Adding a layer to your Lambda Function will unpack the files under / opt at runtime and make them available at runtime. --If you put the module in the directory with the name specified for each language, it will pass the path so that it can be read from the Lambda Function. --For example, in the case of the Python runtime, if you put a Python module in a directory named python in the layer zip, you can load that module with ʻimport` on the Lambda Function side.

For more information: AWS Lambda Layers --AWS Lambda

Create a Docker image to create a layer

The Python 3.8 runtime seems to be running on Amazon Linux 2 [^ runtime], so you should normally set up an EC2 instance on Amazon Linux 2 and create layers in it (although Linux could be anything else). , At least layers made on macOS do not work on AWS Lambda [^ macos]), but in fact Amazon Linux has an official image distributed on Docker Hub [^ dockerhub], so install Python 3.8 based on this If you create a Docker image, you don't have to set up an EC2 instance.

[^ runtime]: AWS Lambda Runtime-AWS Lambda [^ macos]: Mac but I want to AWS Lambda with Python! --Qiita

Dockerfile


FROM amazonlinux:2

ARG PYTHON_VERSION=3.8.2

RUN yum update -y && yum install -y tar gzip make gcc openssl-devel bzip2-devel libffi-devel \
  && curl https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz | tar xz \
  && cd Python-${PYTHON_VERSION} && ./configure && make && make install \
  && cd - && rm -rf Python-${PYTHON_VERSION}

ADD entrypoint.sh /

RUN yum install -y zip \
  && mkdir /python \
  && chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

In Entrypoint, register a script to pip install and zip it.

entrypoint.sh


#!/bin/bash -eu

SRC=/python
DIST=/dist/layer.zip

pip3 install -t ${SRC} $@
rm -f ${DIST}
zip -q -r ${DIST} ${SRC}

Build the image.

$ docker build . -t bundle-pip-modules-for-aws-lambda-layers:python3.8

It seems that the image name is long, but anything is fine here.

Create a Layer with a Docker image

For example, if you want to create a Lambda Layer that can use Pandas.

$ docker run --rm \
    -v $(pwd):/dist \
    bundle-pip-modules-for-aws-lambda-layers:python3.8 \
        pandas

If successful, layer.zip will be created in the current directory.

In the above example, pandas was specified directly, but since all the arguments that can be specified by pip3 install can be passed for the arguments after the image name, for example, mount requirements.txt and then specify the -r option. You can also combine many modules into a layer with.

$ echo 'pandas' > requirements.txt
$ docker run --rm \
    -v $(pwd)/requirements.txt:/requirements.txt \
    -v $(pwd):/dist \
    bundle-pip-modules-for-aws-lambda-layers:python3.8 \
        -r requirements.txt

Register a layer

Upload the created layer.zip.

image.png

Use Layer

As a simplest example, let's create a Lambda Function that does nothing but load Pandas.

lambda_function.py


import pandas

def lambda_handler(event, context):
    pass

Add the registered Layer to the created Lambda Function.

image.png

If you try running this Lambda Function and you don't get an error, it's OK.

Recommended Posts

Create a Layer for AWS Lambda Python with Docker
[For Python] Quickly create an upload file to AWS Lambda Layer
Create a LINE BOT with Minette for Python
Play with Lambda layer (python) for about 5 minutes
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Create a directory with python
Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
[AWS] Create API with API Gateway + Lambda
Create a virtual environment with Python!
AWS Layer Creation Script for python
Notify HipChat with AWS Lambda (Python)
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Create a child account for connect with Stripe in Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple Python development environment with VSCode & Docker Desktop
I just built a virtual environment with AWS lambda layer
Create API with Python, lambda, API Gateway quickly using AWS SAM
[Python] Create a screen for HTTP status code 403/404/500 with Django
Create a Python execution environment for Windows with VScode + Remote WSL
[AWS] Using ini files with Lambda [Python]
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Create a striped illusion with gamma correction for Python3 and openCV3
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a color picker for the color wheel with Python + Qt (PySide)
Create Python + uWSGI + Nginx environment with Docker
Create a word frequency counter with Python 3.4
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Connect to s3 with AWS Lambda Python
Run a Python web application with Docker
Create a web service with Docker + Flask
Create a private repository with AWS CodeArtifact
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Create a USB boot Ubuntu with a Python environment for data analysis
Python + Selenium + Headless Chromium with aws lambda
How to create a serverless machine learning API with AWS Lambda
Let's create a chat function with Vue.js + AWS Lambda + dynamo DB [AWS settings]
Create a frame with transparent background with tkinter [Python]
Periodically run a python program on AWS Lambda
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
Get a ticket for a theme park with python
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
Start a simple Python web server with Docker
[Python] Build a Django development environment with Docker
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
LINE BOT with Python + AWS Lambda + API Gateway
Create a color bar with Python + Qt (PySide)
Create Nginx + uWSGI + Python (Django) environment with docker
Steps to create a Twitter bot with python
Commands for creating a python3 environment with virtualenv
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python