How to deploy a Python Docker image to AWS Lambda?


Introduction

AWS Lambda is a serverless computing platform that allows you to run your code without worrying about infrastructure. Docker is a tool used to package and deploy applications in a standardized and isolated manner. By deploying a Python Docker image to AWS Lambda, you can take advantage of both of these technologies to run your Python code at scale.

Prerequisites

  • AWS account with access to AWS Lambda

  • Docker installation and basic knowledge of Docker commands

  • Python application or code to be packaged in the Docker image

Step 1: Building the Python Docker Image

It would be best if you created a Dockerfile for your Python application to build the Docker image. A Dockerfile is a text file that contains instructions for building the Docker image.

Example

Here is an example Dockerfile for a simple Python application −

#base image we are using is python:3.8-slim
FROM python:3.9
#copy the current working directory to the /app directory inside the container.
COPY . /app
#set /app as the working directory for the python container.
WORKDIR /app
#Install the required packages using the requirement.txt
RUN pip install -r requirements.txt
#set the entry point for the container.
ENTRYPOINT ["python"]
#include the default file to be executed.
CMD ["app.py"]

This Dockerfile uses the official Python Docker image as the base, copies the current directory into the /app directory inside the image, installs the required Python packages specified in requirements.txt, and sets the ENTRYPOINT and CMD to run the app.py file when the image is started. The directory tree is shown below.

├── app.py
└── requirements.txt
|__ Dockerfile
0 directories, 3 files

To build the Docker image, run the commands in the same directory as the Dockerfile −

$ docker build -t my-python-app:latest .

This command will build the Docker image and give it the tag my-python-app:latest.

Output

Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM python:3.9
 ---> 68cf04410baf
Step 2/6 : COPY . /app
 ---> c7d360b69e27
Step 3/6 : WORKDIR /app
 ---> Running in d1182f3361d4
Removing intermediate container d1182f3361d4
 ---> e0a1bc774a95
Step 4/6 : RUN pip install -r requirements.txt
 ---> Running in 10dfa962d575
Removing intermediate container 10dfa962d575
 ---> 32830c9fe0d0
Step 5/6 : ENTRYPOINT ["python"]
 ---> Running in 49db861f7f23
Removing intermediate container 49db861f7f23
 ---> e28b4e30abd7
Step 6/6 : CMD ["app.py"]
 ---> Running in 1e3f5978bac8
Removing intermediate container 1e3f5978bac8
 ---> da008f59919a
Successfully built da008f59919a
Successfully tagged my-python-app:latest

Step 2: Pushing the Docker Image to AWS Elastic Container Registry (ECR)

To deploy the Docker image to AWS Lambda, you first need to push it to the AWS Elastic Container Registry (ECR). ECR is a managed Docker registry service provided by AWS.

To create an ECR repository and push the Docker image to it, follow these steps −

  • Navigate to the ECR dashboard in the AWS Management Console.

  • Click the "Create repository" button.

  • Give the repository a name and click the "Create repository" button.

  • Follow the instructions in the "Push an image" section to authenticate docker with the AWS CLI.

  • Tag the Docker image with the ECR repository URI by running the following command −

$ docker tag my-python-app:latest {your_ecr_repository_uri}
  • Push the Docker image to ECR by running the following command −

$ docker push {your_ecr_repository_uri}

Step 3: Creating the AWS Lambda Function

Now that the Docker image is in ECR, you can create the AWS Lambda function to run it.

  • Navigate to the AWS Lambda dashboard in the AWS Management Console.

  • Click the "Create function" button.

  • Select the "Custom image" option in the "Runtime" field.

  • Specify the ECR repository URI as the image source.

  • Configure the function's triggers and settings as desired.

  • Click the "Create function" button.

Step 4: Testing the AWS Lambda Function

To test that the function works correctly, you can invoke it using the AWS Lambda dashboard or the AWS CLI. Using the AWS Lambda dashboard, you can click the "Test" button and provide any required test event data. If you are using the AWS CLI, you can run the invoke the command and specify the function name and any required event data.

For example, to test the function using the AWS CLI, you can run the following command −

$ aws lambda invoke --function-name my-function --payload '{"key": "value"}' output.txt

This command will invoke the my-function function and pass in the specified event data. The function's output will be saved to the output.txt file.

Once you have verified that the function is working correctly, you can start using it in your application or environment.

Conclusion

In this article, we walked through the steps to deploy a Python Docker image to AWS Lambda. We built the Docker image, pushed it to ECR, and created an AWS Lambda function using the custom image. We also tested the function to make sure it was working correctly.

By deploying a Python Docker image to AWS Lambda, you can use both Docker and AWS Lambda to run your Python code at scale. You can further configure and optimize the function for your specific use case.

Updated on: 16-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements