# Deploying python docker containers on 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`](http://app.py):

```python
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`:

```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):

```bash
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:

```bash
docker tag <container_name>:latest <ecr_repository_name>:latest
```

Now we can finally push our image to AWS ECR:

```bash
docker push <ecr_repository_name>:latest
```

## Lambda

In the AWS Lambda management console, go ahead and create a new function.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1738953157417/d3299249-8293-4b87-924e-e505287e4f78.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1738953185618/9297efc3-ccd0-4767-a8c1-26b7ae630745.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1738953205004/f9c33ca1-c372-4882-9e06-9756d578bbcc.png align="center")

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.
