Skip to content

AWS Lambda Docker: Install pip packages via ECR | ShillehTek

May 23, 2024

Video Tutorial (Optional)

Watch first if you want to follow along with the full workflow for packaging Python dependencies for AWS Lambda using Docker and Amazon ECR.

Project Overview

AWS Lambda + Docker + Amazon ECR: In this guide, you will package Python pip dependencies into a Lambda container image using Docker and push it to Amazon ECR so your Lambda runs with a consistent, reliable runtime environment.

Managing dependencies directly inside a Lambda zip can be cumbersome, especially for larger Python packages. Building a container image with Docker makes installs repeatable, and storing the image in ECR streamlines deployment.

  • Time: 20 to 45 minutes
  • Skill level: Intermediate
  • What you will build: A Lambda function deployed from an ECR-hosted container image that includes your pip packages

Parts List

From ShillehTek

  • None required for this software workflow.

External

  • AWS CLI - used to authenticate and run ECR and Lambda commands
  • Docker (Docker Desktop or Docker Engine) - used to build the Lambda container image locally
  • An AWS account with permissions for ECR and Lambda - required to create repos and deploy images

Note: You must be signed into Docker and have Docker running before performing the ECR login and pushing images.

Step-by-Step Guide

Step 1 - Install and configure the AWS CLI

Goal: Install the AWS CLI and set up credentials so you can use ECR and Lambda from your terminal.

What to do: Install the AWS CLI for your OS, then run the configuration command.

On Windows: Download the AWS CLI MSI installer from the official website and follow the on-screen installer.

On macOS (Homebrew):

brew install awscli

On Linux:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Configure:

aws configure

Expected result: Your AWS Access Key ID, Secret Access Key, default region, and output format are configured for the AWS CLI.

Step 2 - Create an ECR repository

Goal: Create a place in ECR to store your Lambda container image.

What to do: Run the following command (adjust the name and region as needed):

aws ecr create-repository --repository-name my-lambda-repo --region us-east-1

Expected result: An ECR repository exists and is ready to receive Docker image pushes.

Step 3 - Authenticate Docker to ECR

Goal: Allow Docker to push images to your ECR registry.

What to do: Make sure Docker Desktop (or the Docker daemon) is running and you are signed into Docker, then authenticate:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com

Replace <aws_account_id> with your actual AWS account ID.

Expected result: Docker reports a successful login to your ECR registry endpoint.

Step 4 - Create a .dockerignore file

Goal: Keep your Docker build context small by excluding unnecessary files.

What to do: Create a file named .dockerignore in your project directory with the following contents:

__pycache__
.git
tests
*.md
*.txt

Expected result: Docker excludes these files and folders during docker build.

Step 5 - Create the Dockerfile for the Lambda image

Goal: Define the Lambda container image and install the pip packages into the image.

What to do: Create a Dockerfile in your project directory:

# Builder stage - you can use other AWS runtimes and python versions
FROM public.ecr.aws/lambda/python:3.12-x86_64 as builder

# Install Python packages (replace as needed)
RUN pip install --no-cache-dir requests numpy pandas --target "/var/task"

# Final stage
FROM public.ecr.aws/lambda/python:3.12-x86_64

# Copy necessary files from the builder stage - add whatever code you need
COPY --from=builder /var/task /var/task
COPY handler.py ./

# Set the CMD to your handler
CMD ["handler.lambda_handler"]

Expected result: Your Dockerfile is ready to build a Lambda-compatible container image with your dependencies included.

Step 6 - Build the Docker image

Goal: Produce a local Docker image that includes your Lambda code and pip dependencies.

What to do: Build the image from the directory containing your Dockerfile:

docker build -t my-lambda-image .

Expected result: A local Docker image named my-lambda-image is created.

Step 7 - Tag the image for ECR

Goal: Apply an ECR-compatible tag so the image can be pushed to your repository.

What to do:

docker tag my-lambda-image <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest

Expected result: Your image has an additional tag pointing at your ECR repository and image name.

Step 8 - Push the image to ECR

Goal: Upload the container image so Lambda can pull and run it.

What to do:

docker push <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest

Expected result: The image appears in your ECR repository with the latest tag.

Step 9 - Update the Lambda function to use the new image

Goal: Deploy your updated dependencies by switching the Lambda code source to your ECR image.

What to do: Update your existing Lambda function using the AWS CLI:

aws lambda update-function-code --function-name my-lambda-function --image-uri <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest

Replace my-lambda-function with the name of your Lambda function.

Alternatively, in the AWS Management Console:

  1. Navigate to the AWS Lambda console.
  2. Select your Lambda function from the list.
  3. In the "Code" tab, choose "Image" as the code source.
  4. Select the ECR image you pushed earlier.
  5. Save the changes.

Expected result: Your Lambda function is updated to run from the ECR container image that includes your pip packages.

Step 10 - Create a new Lambda function from a container image (optional)

Goal: Create a brand new Lambda that uses your Docker image from the start.

What to do: In the AWS Management Console:

  1. Navigate to the AWS Lambda console.
  2. Click on "Create function".
  3. Select "Container image" as the code source.
  4. Select the ECR repository and image tag you pushed earlier.
  5. Configure the remaining settings (function name, role, etc.).
  6. Click on "Create function".

Expected result: A new Lambda function is created with your ECR-hosted container image as its execution environment.

Conclusion

You packaged pip dependencies for AWS Lambda using Docker and deployed them via Amazon ECR, which helps ensure a consistent runtime environment and more reliable deployments. This workflow makes it easier to manage complex Python dependencies compared to manual zip packaging.

Want the exact parts used in your next embedded build? Grab them from ShillehTek.com. If you want help building or customizing an IoT or cloud-connected solution, check out our IoT consulting services.

If you want to follow along with more tutorials, you can subscribe on Youtube, support at https://www.buymeacoffee.com/mmshilleh, or hire via https://www.upwork.com/freelancers/~017060e77e9d8a1157.