<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Devin</title>
    <description>The latest articles on Forem by Devin (@dev_insights).</description>
    <link>https://forem.com/dev_insights</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3005835%2F897856f8-afa7-49b5-b026-69949cb3ecc7.jpeg</url>
      <title>Forem: Devin</title>
      <link>https://forem.com/dev_insights</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dev_insights"/>
    <language>en</language>
    <item>
      <title>How to deploy FastAPI on AWS lambda with docker in 2025</title>
      <dc:creator>Devin</dc:creator>
      <pubDate>Tue, 03 Jun 2025 20:26:28 +0000</pubDate>
      <link>https://forem.com/dev_insights/deploy-fastapi-on-aws-lambda-with-docker-2025-3a3a</link>
      <guid>https://forem.com/dev_insights/deploy-fastapi-on-aws-lambda-with-docker-2025-3a3a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Modern serverless architectures have transformed how developers build and deploy applications, offering unprecedented scalability and cost-efficiency. &lt;strong&gt;FastAPI&lt;/strong&gt; with &lt;strong&gt;AWS Lambda&lt;/strong&gt;, 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.&lt;/p&gt;

&lt;p&gt;This guide will help you deploy a FastAPI application on an AWS Lambda and make it available to the public.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Python 3.9+&lt;/strong&gt; (we’ll use Python 3.12 in this tutorial, but any 3.9+ version works).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker Desktop&lt;/strong&gt; (to build and test container images locally).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS account&lt;/strong&gt; with permissions to create ECR repositories and Lambda functions. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  FastAPI Setup
&lt;/h2&gt;

&lt;p&gt;Create a directory for project and isolate dependencies in a virtual environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make a new directory&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir FastApiProject
cd FastApiProject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can use GUI to do the same as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Create a virtual environment&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m venv .venv
.venv\Scripts\activate.bat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install FastAPI and Mangum in the virtual environment&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install "fastapi[standard]"
pip install mangum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;"fastapi[standard]" installs FastAPI itself, plus Uvicorn (an ASGI server), pydantic, and other tools.&lt;/p&gt;

&lt;p&gt;mangum is a lightweight adapter that lets AWS Lambda invoke your ASGI app seamlessly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Create requirements.txt in root directory&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create the app directory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your project tree should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FastApiProject/
├── .venv/
├── requirements.txt
└── app/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the app/ folder, create main.py and insert following code inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Run the FastAPI app locally to verify it works&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fastapi dev app/main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;API should now be accessible at &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also open the browser and navigate to &lt;a href="http://localhost/docs" rel="noopener noreferrer"&gt;http://localhost/docs&lt;/a&gt; to see the interactive Swagger UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup the Mangum handler for Lambda&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from mangum import Mangum

handler = Mangum(app, lifespan="off")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final main.py will now look like this&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Setup
&lt;/h2&gt;

&lt;p&gt;Create a &lt;strong&gt;Dockerfile&lt;/strong&gt; for Containerized Deployment and insert the following code inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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"]  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ECR Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create an ECR repository&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Amazon ECR → Repositories.&lt;/li&gt;
&lt;li&gt;Click Create repository.&lt;/li&gt;
&lt;li&gt;Give it a name, e.g., fastapi-project.&lt;/li&gt;
&lt;li&gt;Leave all other settings as default and click Create repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Upload Project to ECR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the repository details page, click View push commands. &lt;br&gt;
Here, You will see 4 commands&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;first, is used to login to AWS account&lt;/li&gt;
&lt;li&gt;second, builds the project (docker desktop must be started to finish this step)&lt;/li&gt;
&lt;li&gt;third, tags the image with latest&lt;/li&gt;
&lt;li&gt;fourth, push the image to ECR&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After a few moments, you should see the image layers being uploaded. &lt;/p&gt;

&lt;p&gt;In the ECR console, the fastapi-project repository will list the image with latest tag.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lambda Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create the AWS Lambda Function from the Container Image&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the AWS Console, go to AWS Lambda → Functions, then click Create function.&lt;/li&gt;
&lt;li&gt;Select “Container image” as the deployment type.&lt;/li&gt;
&lt;li&gt;Configure function

&lt;ul&gt;
&lt;li&gt;Function name: fastapi-function (or any descriptive name).&lt;/li&gt;
&lt;li&gt;Container image URI: Click Browse images, navigate to ECR, and pick the fastapi-project:latest image.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click “Create function” and wait a few seconds while AWS provisions resources.&lt;/li&gt;
&lt;li&gt;it will start the process, wait for few seconds for process to finish. it depends on the size of project.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now to test if deployment worked, we will configure a Function URL (public HTTP endpoint):&lt;/p&gt;

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

&lt;p&gt;AWS will generate a HTTPS URL which will be visible on function's home page.&lt;/p&gt;

&lt;p&gt;Clicking this URL opens a new page that displays the output on-screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "message": "Hello, World from FastAPI on AWS Lambda!"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this we have just built an end-to-end, production-ready FastAPI service running on AWS Lambda. &lt;/p&gt;

&lt;p&gt;By following this guide, you now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initialized a FastAPI project in a clean directory with virtual environment isolation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Developed a minimal FastAPI application and tested it locally with Uvicorn.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Containerized the app with an optimized Dockerfile, leveraging AWS’s official Lambda Python base image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pushed the container image to Amazon ECR and created a Lambda function that references it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exposed the FastAPI endpoints publicly via a Lambda Function URL for easy testing.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Happy deployment&lt;/strong&gt; 😊&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps and Best Practices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;API Gateway vs. Function URL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Environment Variables &amp;amp; Configuration&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;If you liked this post or have any questions/comments, please leave a comment below!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>docker</category>
      <category>lambda</category>
    </item>
  </channel>
</rss>
