No servers to patch, nothing to pay for while idle. Package a FastAPI project as a Lambda function — code in one archive, dependencies in a reusable layer.
AWS Lambda runs code on demand and bills only while it executes. FastAPI is an ASGI application, and Lambda speaks in events — so we need an adapter between the two. That adapter is Mangum.
This guide deploys a project using two separate archives: the application code, and its dependencies packaged as a Lambda layer. Keeping them apart means code-only changes upload in seconds, and the layer can be shared between functions.
Before you start
You’ll need a FastAPI project and an AWS account with access to Lambda and S3. The examples use a project called sample-fastapi-project, an S3 bucket called sample-fastapi-s3, and a function called sample-fastapi-lambda.
Prepare the project
Isolate the project’s dependencies in a virtual environment and install them:
Field notes, deployment guides and research from the engineers who build web, cloud and mobile products at Aviox Blogs.
Insights & Understanding
Frequently Asked Questions
Answers to common questions regarding this article's topic, architecture, and practical applications.
AWS Lambda runs code on demand and bills only while it executes. FastAPI is an ASGI application, and Lambda speaks in events — so we need an adapter between the two. That adapter is Mangum.
Definition / Understanding
Under Configuration → Function URL, create a function URL. Open it in a browser with /docs appended and FastAPI’s interactive documentation should load — served entirely by Lambda. An auth type of NONE means public A function URL with auth type NONE is reachable by anyone on the internet. That’s fine for a public API, but protect private endpoints with IAM auth, an API gateway, or application-level authentication. From here, redeploying is a matt
General
Under Configuration → General configuration, a sensible starting point is: Add any environment variables your application expects under Environment variables.
General
Lambda looks for layer packages inside a top-level python/ directory. Install into that directory and zip it: pip install -r requirements.txt \
--platform manylinux2014_x86_64 --only-binary=:all: \
--target python/
zip -r9 requirements.zip python
Build for Linux FastAPI depends on Pydantic, which ships compiled code. If you package on macOS or Windows without the --platform flag, the layer will contain the wrong binaries and the function will
General
Isolate the project’s dependencies in a virtual environment and install them: python3 -m venv venv
source venv/bin/activate # Windows: .\venv\Scripts\activate
pip install -r requirements.txt
Add Mangum and record it in your requirements: pip install mangum
pip freeze > requirements.txt
Then wrap the application. The handler object is what Lambda will invoke: