DEV Community

Tien Ho
Tien Ho

Posted on

How to deploy layer python for aws lambda

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"]

Enter fullscreen mode Exit fullscreen mode

Step 2: Build and Run the Docker Container

  1. Build the Docker image:
docker build -t lambda-python-layer .
Enter fullscreen mode Exit fullscreen mode
  1. Run the Docker container and output the layer zip file:
docker run --rm -v $(pwd):/output lambda-python-layer
Enter fullscreen mode Exit fullscreen mode

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:

  1. Go to the AWS Lambda console.
  2. Navigate to the "Layers" section.
  3. Click "Create layer."
  4. Upload the layer.zip file and select the appropriate runtime (Python 3.11).

Top comments (0)