This project provides a Docker-based environment for building AWS Lambda layers with Python dependencies. It simplifies the process of creating Lambda layers that are compatible with the AWS Lambda runtime environment.
aws-lambda-layer-creator/
├── Dockerfile
├── requirements.txt
└── output/
Dockerfile: Contains the Docker configuration for building the Lambda layer.requirements.txt: Lists Python dependencies to be included in the layer.output/: Directory where the generated Lambda layer ZIP file will be stored.
- Docker installed on your system.
- Basic understanding of AWS Lambda layers.
- AWS CLI (for deploying the layer to AWS).
The requirements.txt in this project currently includes:
polars
You can customize requirements.txt to include any other packages you need.
The Dockerfile performs the following steps to create the Lambda layer:
- Uses the official
python:3.13-slimimage as a lightweight base. - Installs the
ziputility, which is necessary for packaging the layer. - Sets up an internal working directory (
/buildarea) where the layer components will be assembled. This directory is distinct from the final output mount point to avoid conflicts. - Copies your
requirements.txtfile into this build area. - Installs the Python packages listed in
requirements.txtdirectly into apythonsubdirectory within/buildarea. It uses specificpip installflags (--platform manylinux2014_x86_64,--implementation cp,--python-version 3.11,--only-binary=:all:,--target=python) to ensure the packages are compatible with the AWS Lambda execution environment for Python 3.13. - Archives the
pythonsubdirectory (containing all installed dependencies) into alambda-layer.zipfile located within the/buildarea. - The container's default command (
CMD) is configured to copy thislambda-layer.zipfrom/buildareato the/lambda-layer/directory. When you run the container with a volume mount (as shown in the Usage section),/lambda-layer/points to your localoutputdirectory, making the ZIP file available on your host machine.
-
Build the Docker image: This command builds the Docker image and tags it as
lambda-layer-builder. It also includes a step to prune any dangling images after a successful build to keep your system clean.docker build --platform="linux/amd64" -t lambda-layer-builder . && docker image prune -f
-
Run the container to generate the layer: This command runs the
lambda-layer-buildercontainer.--platform="linux/amd64"ensures the build is for the Lambda environment.--rmautomatically removes the container when it exits.-v $(pwd)/output:/lambda-layermounts your local./outputdirectory to/lambda-layerinside the container. The generated ZIP file will be copied here.
docker run --platform="linux/amd64" --rm -v $(pwd)/output:/lambda-layer lambda-layer-builder
-
Retrieve the layer: The
lambda-layer.zipfile will be created in your localoutputdirectory. -
Deploy to AWS: Upload the generated
lambda-layer.zipfile to AWS Lambda as a new layer version via the AWS Management Console, AWS CLI, or your preferred Infrastructure as Code tool.
- The layer is built specifically for Python 3.11.
- Packages are compiled for the
manylinux2014_x86_64platform to ensure compatibility with AWS Lambda. - You can customize the
requirements.txtto include any dependencies your Lambda function needs. - Always be mindful of AWS Lambda layer size limits (currently 250 MB unzipped for the function and all layers combined).
To modify the included Python dependencies:
- Edit the
requirements.txtfile to add, remove, or change package versions. - Rebuild the Docker image using the command in Step 1 of the "Usage" section.
- Run the container again (Step 2 of "Usage") to generate a new
lambda-layer.zipfile with your updated dependencies.