Creating an AWS Lambda Python Layer Using Docker
Here's how you can create a Dockerfile and Docker command to build an AWS Lambda Python layer using the public.ecr.aws/lambda/python:3.11
image.
Step 1: Create the Dockerfile
# Use the official AWS Lambda Python 3.11 image as the base image
FROM public.ecr.aws/lambda/python:3.11
# Set the working directory inside the container
WORKDIR /lambda-layer
# Copy the requirements.txt file (if you have one) into the container
COPY requirements.txt .
# Install the Python packages into the /lambda-layer/python directory
RUN pip install -r requirements.txt -t /lambda-layer/python
# Package the content of the /lambda-layer directory as a zip file
RUN zip -r9 /layer.zip /lambda-layer
# Set the default command to output the layer zip file
CMD ["cat", "/layer.zip"]
Step 2: Build and Run the Docker Container
- Build the Docker image:
docker build -t lambda-python-layer .
- Run the Docker container and output the layer zip file:
docker run --rm -v $(pwd):/output lambda-python-layer
This command will run the container, generate the layer.zip file inside the container, and then copy it to your current directory.
Step 3: Deploy the Layer to AWS Lambda
Once you have the layer.zip file, you can upload it to AWS Lambda as a new layer:
- Go to the AWS Lambda console.
- Navigate to the "Layers" section.
- Click "Create layer."
- Upload the layer.zip file and select the appropriate runtime (Python 3.11).
Top comments (0)