Skip to content

How do I use an Amazon ECR image and Docker to create a Lambda layer?

3 minute read
0

I want to use an Amazon Elastic Container Registry (Amazon ECR) image and Docker to create an AWS Lambda layer that's compatible with my environment runtimes.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version. The following resolution uses Python and Linux. The steps might vary for other coding languages and operating systems (OS).

Prerequisite: Install Docker on your computer. For installation steps, see Get Docker and Install Docker Engine (Linux only) on the Docker Docs website.

Create the directory structure and specify your dependencies

Create the directory structure and specify your dependencies in the requirements.txt pip requirements file. For more information about how to specify dependencies, see Requirements files on the pip website.

The following example directory structure is for a Lambda layer that's compatible with Python 3.x:

my-layer/ 
├── requirements.txt 
└── python/

Note: The total unzipped size of the function and all layers can't exceed the unzipped deployment package size limit of 250 MB. For information about Python version support in Lambda, see Building Lambda functions with Python.

Install the library dependencies to their subfolders

Run the following command once for each runtime that you specified in the directory structure:

docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.x" /bin/sh -c "pip install -r requirements.txt -t python/; exit"

Note: Replace 3.x with the compatible Python library that you want to install.

Create a .zip file archive for your layer

To zip the python folder and create the layer package, run the following command:

zip -r mypythonlibs.zip python > /dev/null

Note: Replace mypythonlibs with a name for your layer package.

Create or update your layer

Run the following publish-layer-version AWS CLI command:

aws lambda publish-layer-version --layer-name mypythonlibs --description "My python libs" --zip-file fileb://mypythonlibs.zip --compatible-runtimes "python3.x"

Note: Replace mypythonlibs with your layer package name, My python libs with a description of the layer package, and python3.x with the installed Python library.

In the output, note the layer's Amazon Resource Name (ARN).

Update your Lambda function configuration to use the layer

Run the following update-function-configuration command:

aws lambda update-function-configuration --layers arn:aws:lambda:us-east-2:123456789012:layer:mypythonlibs:1 --function-name my-function

Note: Replace arn:aws:lambda:us-east-2:123456789012:layer:mypythonlibs:1 with the layer ARN and my-function with the name of your function. The preceding command places the layer in the /opt folder of the Lambda environment. You can access the /opt folder from your Lambda function code.

Related information

How do I resolve "unknown service", "parameter validation failed", or "object has no attribute" errors from a Python (Boto3) Lambda function?

New for AWS Lambda – use any programming language and share common components

Amazon ECR Public Gallery

AWS OFFICIALUpdated 7 months ago
1 Comment

I followed the steps as described in the article. Used docker image sam/build-python3.10 to install dependencies, deployed the layer and updated the Lambda with runtime python3.10 to use the layer. It works fine when the layer is deployed from a Windows machine through WSL (Ubuntu 22.04.3 LTS). When the exact same deployment with the same docker image is ran from a Mac machine, the Lambda fails with an ImportModuleError: cannot import name 'etree' from 'lxml' (/opt/python/lib/python3.10/site-packages/lxml/init.py)

EDIT: Always remember to check the target architecture in addition to the runtime! Updated the docker image to sam/build-python3.10:latest-x86_64 and now the error is gone!

replied a year ago