Create a Lambda layer for a Python 3.9 Package

Lambda layers provide a convenient way to package libraries and other dependencies that you can use with your Lambda functions. Using layers reduces the size of uploaded deployment archives and makes it faster to deploy your code. Thit sheet will help you to create a layer for one single library.

Install Python on linux2 ec2

follow the instructions on: Install_Python_on_linux2_ec2

On my Cloud9 machine I created a folder: layers and in that folder I should create the folder for the package what I will build the layer for.

Create folderstructure

mkdir layers
cd layers
mkdir package_name
cd package_name

Create python folder

Create a python folder by running the following command, but do not CD into it!

mkdir python

Install the library

Install the _xxx_ library files into the python folder by running the following command:

python3.9 -m pip install  _xxx_ -t python/

Important: Replace _xxx_ with the name of the Python library that you want to import.

Package the library for upload

Zip the contents of the python folder into a layer.zip file by running the following command:

zip -r layer.zip python

Publish the library to AWS Lambda

Publish the Lambda layer by running the following command:

Important: Replace us-east-1 with the AWS Region that your Lambda function is in.

aws lambda publish-layer-version --layer-name pandas-layer --zip-file fileb://layer.zip --compatible-runtimes python3.9 --region us-east-1

Add the layer to your Lambda function.