Skip to content

Lambda Layers

Uzair Syed edited this page Sep 25, 2025 · 1 revision

Creating Lambda Layers

This section discusses how lambda layers can be used to package dependencies for developers. There are two methods to packaging the libraries to AWS:

  1. Downloading the .whl file and uploading
  2. Using Docker to package the zip files and uploading

Folder Structure Requirement

To upload the lambda layers to enable the lambda functions for using those layers, we can simply download the content of the libraries onto our machine and zipping them. The folder structure to package the lambda layer needs to be of following:

python/              # Required top-level directory
 └── requests/
 └── boto3/
 └── numpy/
 └── (dependencies of the other packages) 

Step 1: Creating ZIP

We will use Numpy as an example to create a lambda layer for. We will use Python 3.13 interpreter and require the manylinux wheel format for the x86-64 architecture.

Option 1: Navigate to the download files

  • Navigate to https://pypi.org/project/numpy/#files
  • Download the correct .whl file. You will see the CPython 3.13tmanylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64 as the description
  • Navigate to the downloads folder where you downloaded the file. Move it to another folder where you can create the layer
{EED815D9-89A2-4268-944E-BD7214B44D14}
  • Use 7-Zip extension to unzip the contents (or you can use any other method). If you do not have the 7-Zip extension, you can download it from https://www.7-zip.org/download.html).
  • You can unzip by right clicking on the .whl file -> Open With -> 7-Zip File Manager.
  • Click on Extract
{DF44BA43-2DC2-4BA6-9A64-5E10F9E35D69}
  • Ensure the contents extracted are under the Python folder. Click Ok.
{57682DD3-F46D-404F-AE43-3C077CBFA344}
  • Navigate to the directory the contents were extracted to. Right-click on the python folder (this was the folder that the contents were extracted into), and compress to ZIP.

Option 2: Use Docker to create the ZIP

  • Navigate to a directory where you will create the lambda layer using Docker
  • Using the terminal, create a directory called numpy-lambda-layer and cd into the directory. This is where the Dockerfile will be stored and the final zipped layer.
mkdir numpy-lambda-layer
cd numpy-lambda-layer
  • Create a file name Dockerfile and copy the following code.
# Stage 1: The build environment
FROM public.ecr.aws/lambda/python:3.13 AS builder

# Install the 'zip' utility
RUN dnf install -y zip

# Create the directory structure for the Lambda layer
RUN mkdir -p /opt/python

# Install numpy into the layer directory
# Note: numpy-binary does not exist; install numpy directly
RUN pip install --platform manylinux2014_x86_64 --target /opt/python --python-version 3.13 numpy

# Create a zip file containing the layer content
RUN cd /opt && zip -r /numpy-layer.zip .

# Stage 2: The final, minimal image
FROM scratch

# Copy the zip file from the 'builder' stage to the final image
COPY --from=builder /numpy-layer.zip /numpy-layer.zip
  • Build the Docker image by running the following in the command terminal from the directory containing your Dockerfile. This will build the Docker image with numpy installed. docker build -t numpy-builder .

  • After the build above completes, extract the numpy-layer.zip using the following Docker commands below. This will create a numpy-layer.zip file in your current directory, ready for upload to AWS Lambda.

# Create a temporary container from the 'builder' stage
docker create --name temp-container numpy-builder --from builder

# Copy the zipped layer file from the container to your local machine
docker cp temp-container:/numpy-layer.zip .

# Remove the temporary container
docker rm temp-container

Note: When creating a Lambda Layer for psycopg2 or any other libraries, you will need to update the Dockerfile with the appropriate requirements.


Step 2: Upload to AWS

  • Log into your AWS Console
  • Go to Lambda service
  • Go to Layers on the left hand side menu
  • Click on Create Layer
  • Add the details for your Lambda Layer, and upload the ZIP file from the directory you created the ZIP for the library.
  • Note: Ensure the Compatible Architecture and the Compatible Runtimes match what was used when creating the ZIP layer.
  • Click on Create to create the Layer
{C32D0303-C1C3-4331-AED1-D9AF33E4D51D}

Clone this wiki locally