DEV Community

Cover image for How to deploy FastAPI on AWS lambda with docker in 2025
Devin
Devin

Posted on

How to deploy FastAPI on AWS lambda with docker in 2025

Introduction

Modern serverless architectures have transformed how developers build and deploy applications, offering unprecedented scalability and cost-efficiency. FastAPI with AWS Lambda, combines the power of Python's fastest web framework with serverless computing, enabling you to build production-ready APIs that scale automatically and minimize infrastructure costs.

This guide will help you deploy a FastAPI application on an AWS Lambda and make it available to the public.

Prerequisites

  1. Python 3.9+ (we’ll use Python 3.12 in this tutorial, but any 3.9+ version works).
  2. Docker Desktop (to build and test container images locally).
  3. AWS account with permissions to create ECR repositories and Lambda functions.

FastAPI Setup

Create a directory for project and isolate dependencies in a virtual environment.

Make a new directory

mkdir FastApiProject
cd FastApiProject
Enter fullscreen mode Exit fullscreen mode

You can use GUI to do the same as well.

Create a virtual environment

python -m venv .venv
.venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

Install FastAPI and Mangum in the virtual environment

pip install "fastapi[standard]"
pip install mangum
Enter fullscreen mode Exit fullscreen mode

"fastapi[standard]" installs FastAPI itself, plus Uvicorn (an ASGI server), pydantic, and other tools.

mangum is a lightweight adapter that lets AWS Lambda invoke your ASGI app seamlessly.

Create requirements.txt in root directory

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Create the app directory

Your project tree should now look like this:

FastApiProject/
├── .venv/
├── requirements.txt
└── app/
Enter fullscreen mode Exit fullscreen mode

Inside the app/ folder, create main.py and insert following code inside it:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, World from FastAPI on AWS Lambda!"}
Enter fullscreen mode Exit fullscreen mode

Run the FastAPI app locally to verify it works

fastapi dev app/main.py
Enter fullscreen mode Exit fullscreen mode

API should now be accessible at http://localhost:8000

You can also open the browser and navigate to http://localhost/docs to see the interactive Swagger UI.

Setup the Mangum handler for Lambda

from mangum import Mangum

handler = Mangum(app, lifespan="off")
Enter fullscreen mode Exit fullscreen mode

Final main.py will now look like this

from fastapi import FastAPI
from mangum import Mangum

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, World from FastAPI on AWS Lambda!"}

handler = Mangum(app, lifespan="off")
Enter fullscreen mode Exit fullscreen mode

Docker Setup

Create a Dockerfile for Containerized Deployment and insert the following code inside it.

# Use the official AWS Lambda Python 3.12 base image
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements and install dependencies
COPY requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip install -r requirements.txt

# Copy application code into the container
COPY ./app ${LAMBDA_TASK_ROOT}/app

# Specify the Lambda function handler (app.main.handler) 
CMD ["app.main.handler"]  
Enter fullscreen mode Exit fullscreen mode

ECR Setup

Create an ECR repository

  • Go to Amazon ECR → Repositories.
  • Click Create repository.
  • Give it a name, e.g., fastapi-project.
  • Leave all other settings as default and click Create repository.

Upload Project to ECR

In the repository details page, click View push commands.
Here, You will see 4 commands

  1. first, is used to login to AWS account
  2. second, builds the project (docker desktop must be started to finish this step)
  3. third, tags the image with latest
  4. fourth, push the image to ECR

After a few moments, you should see the image layers being uploaded.

In the ECR console, the fastapi-project repository will list the image with latest tag.

Lambda Setup

Create the AWS Lambda Function from the Container Image

  1. In the AWS Console, go to AWS Lambda → Functions, then click Create function.
  2. Select “Container image” as the deployment type.
  3. Configure function
    • Function name: fastapi-function (or any descriptive name).
    • Container image URI: Click Browse images, navigate to ECR, and pick the fastapi-project:latest image.
  4. Click “Create function” and wait a few seconds while AWS provisions resources.
  5. it will start the process, wait for few seconds for process to finish. it depends on the size of project.

Now to test if deployment worked, we will configure a Function URL (public HTTP endpoint):

  • In your function’s Configuration tab, click Function URL on the left sidebar.
  • Click Create function URL.
  • Auth type: Choose NONE if you want an open, public endpoint (for testing/prototyping). For production, consider using AWS_IAM or a custom authorizer.
  • Click Save.

AWS will generate a HTTPS URL which will be visible on function's home page.

Clicking this URL opens a new page that displays the output on-screen:

{
  "message": "Hello, World from FastAPI on AWS Lambda!"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this we have just built an end-to-end, production-ready FastAPI service running on AWS Lambda.

By following this guide, you now:

  1. Initialized a FastAPI project in a clean directory with virtual environment isolation.

  2. Developed a minimal FastAPI application and tested it locally with Uvicorn.

  3. Containerized the app with an optimized Dockerfile, leveraging AWS’s official Lambda Python base image.

  4. Pushed the container image to Amazon ECR and created a Lambda function that references it.

  5. Exposed the FastAPI endpoints publicly via a Lambda Function URL for easy testing.

Happy deployment 😊

Next Steps and Best Practices

API Gateway vs. Function URL

Function URL is the quickest way to expose Lambda-backed API, but it lacks advanced features like WAF integration, custom domain mapping, or request/response transformations.

If you need features like API key management, request throttling, or granular resource policies, integrate your Lambda with Amazon API Gateway instead of using the built-in Function URL. The setup is similar: create an API Gateway REST or HTTP API, point the integration to your Lambda function, and configure routes.

Environment Variables & Configuration

Use Lambda’s Environment Variables (in the Configuration tab) to inject secrets or configuration at runtime (e.g., database URLs, third-party API keys).

If you liked this post or have any questions/comments, please leave a comment below!

Top comments (0)