Deploying python docker containers on aws lambda
Learn how to make Python run without servers using AWS Lambda

In this article, we are going to go through how to deploy your Python Docker containers to AWS Lambda.
Architecture of AWS Lambda
AWS Lambda architecture is slightly different from traditional server architecture. It runs functions as code, meaning you define your logic in a function of code and let Lambda take care of deploying and running your code.
So, let’s get started.
Code
In app.py:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({"message": "Hello from AWS Lambda!"})
}
In Dockerfile:
FROM public.ecr.aws/lambda/python:3.11
COPY app.py /var/task/
CMD ["app.lambda_handler"]
Here, the Python base image must be pulled from AWS ECR. We have to log in to Docker with AWS ECR (assuming the reader has AWS account access):
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <aws-account-id>.dkr.ecr.<your-region>.amazonaws.com
Replace your account ID and your region in this command, and you'll be good to go.
Now, tag the Docker image you just built with the repository name in AWS ECR:
docker tag <container_name>:latest <ecr_repository_name>:latest
Now we can finally push our image to AWS ECR:
docker push <ecr_repository_name>:latest
Lambda
In the AWS Lambda management console, go ahead and create a new function.

Click on "Create function," choose the container image option, and select the ECR repository from the dropdown.


That’s it! Your function is now ready.
We can also check its functionality by clicking on the test button to make sure everything is working as expected.
Now that we have set up our Lambda function, we need to make the function available to the public internet. To do this, we are using AWS API Gateway.