Dockerizing a Python Lambda Function on AWS: A Step-by-Step Guide

Dockerizing a Python Lambda function can be a powerful way to manage dependencies and simplify deployment processes. In this hands-on tutorial, we’ll guide you through the process of containerizing a Python Lambda function and deploying it on AWS using Amazon Elastic Container Registry (ECR) and AWS Lambda. Let’s get started!


Before we dive into the hands-on section, make sure you have the following prerequisites in place:

  1. An AWS account with sufficient permissions.
  2. An EC2 instance set up with AWS CLI configured.
  3. A Python Lambda function you want to Dockerize.

Step 1: Setting Up the Project Structure

First, create a folder for your project. In this example, we’ll name it “python_test.” Within this folder, you should have three essential files:

  • This is the Python Lambda function you want to containerize.
  • Dockerfile: The Dockerfile that defines how to build the Docker image.
  • requirements.txt: A file specifying any Python dependencies your Lambda function relies on.

Ensure that your contains the Lambda function you want to containerize, and the Dockerfile and requirements.txt files are correctly set up.

Step 2: Creating an Amazon ECR Repository

Now, let’s create an Amazon ECR repository to host our Docker image. Follow these steps:

  1. Log in to your AWS account and navigate to Amazon ECR.
  2. Click on “Create Repository.”
  3. Choose a repository name, e.g., “python_lambda_container.”
  4. Set the repository to be private (if needed).
  5. Click on “Create Repository.”

Your ECR repository is now ready to host your Docker image.

Step 3: Dockerizing the Lambda Function

Next, let’s build and push our Docker image to ECR. Execute the following commands on your EC2 instance:

# Log in to AWS ECR
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>

# Build the Docker image
docker build -t python_lambda_container .

# Tag the Docker image
docker tag python_lambda_container:latest <your-account-id>.dkr.ecr.<your-region>

# Push the Docker image to ECR
docker push <your-account-id>.dkr.ecr.<your-region>

Replace <your-region> and <your-account-id> with your AWS region and account ID.

Step 4: Creating an AWS Lambda Function

Now, let’s create an AWS Lambda function that uses our Dockerized image:

  1. Go to the AWS Lambda console.
  2. Click “Create function.”
  3. Choose “Container image” as the runtime.
  4. Provide a function name, e.g., “python_lambda_function.”
  5. In the “Container image URI” field, enter the URI of the Docker image you pushed to ECR.
  6. Leave the rest of the settings as default.
  7. Click “Create function.”

Your Lambda function is now set up to run using your Dockerized image.

Step 5: Testing the Lambda Function

Finally, let’s test our Lambda function:

  1. In the Lambda console, go to your function.
  2. Click “Test” and select “Create new test event.”
  3. Leave the event template as is, or customize it as needed.
  4. Click “Create” and then “Test.”

You should see a successful response with a status code of 200 and the expected message from your Lambda function.


Congratulations! You’ve successfully Dockerized a Python Lambda function and deployed it on AWS using Amazon ECR and AWS Lambda. This approach simplifies dependency management and enhances the deployment process for your serverless applications. Feel free to explore further and adapt this tutorial to your specific use cases. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top