Introduction
This guide outlines the steps required to deploy a FastAPI project on AWS Lambda using a separate requirements package and code-only zip file.
Prerequisites:
Before you begin, you will need the following:
- A sample FastAPI project
- An AWS account with access to AWS Lambda and S3 services to deploy code
For this example, we will use a FastAPI project named sample-fastapi-project
and an S3 bucket named sample-fastapi-s3
. We will also create an AWS Lambda function named sample-fastapi-lambda
.
Step 1 — Create a virtual environment to isolate our app dependencies.
$ python3 -m venv venv/
Step 2 – Activate the environment:
# In Linux, type
$ source venv/bin/activate
# In Windows, type
$ .\venv\Scrips\activate
Step 3 – Install dependencies that are required for this project:
$ pip install -r requiements.txt
Step 4 – Add Mangum to the FastAPI code:
$ pip install mangum
$ pip freeze > requiements.txt
Import the Mangum module in your FastAPI code:
from mangum import Mangum
Wrap the FastAPI app with Mangum:
app = FastAPI()
handler = Mangum(app)
Mangum allows us to wrap the API with a handler that we will package and deploy as a Lambda function in AWS.
Step 5 – Create a separate requirements package zip file
$ pip3 install -r requirements.txt -t ./packages
$ cd packages
$ zip -r9 ../requirements.zip .
This will create a seperate requirements.zip file
Step 6 – Create a code-only zip file
Create a separate zip file containing only your project’s code, including the main.py
file. Name this file sample-fastapi.zip
.
# if all the code is in main.py file, then
$ zip sample-fastapi.zip main.py
# Add other files to created zip if required
$ zip sample-fastapi.zip -u <python_file>
Step 7 – Upload the requirements package and code-only zip files to S3(sample-fastapi-s3):
Go to the s3 bucket and click on upload icon to uplaod both files directly.
Step 8 – Create a new Lambda function:
- Navigate to the Lambda service in the AWS Console and click
Create function
. - Select
Author from scratch
and entersample-fastapi-lambda
as the function name. - Choose
Python 3.7
as the runtime. - If having existing role then under
Change default execution role
ChooseExecution role
asUse an existing role
and select the role - Otherwise Choose
Create a new role with basic Lambda permissions
and Clickcreate function
Step 9 – Add the requirements package as a Lambda layer:
- In the Lambda function page, navigate to the
Layers
section and clickAdd a layer
>Layers
>create layer
. - Choose
Upload a file from Amazon S3
and copy theS3 URI
of therequirements.zip
file that you uploaded to S3 in Step 7. - Enter a name for the layer and click
Create
.
Step 10 – Add the code-only zip file to the Lambda function:
- In the Lambda function page, navigate to the
Function code
section and choose Upload fromAmazon S3 location
underCode source
. - Copy the
S3 ULI
ofsample-fastapi.zip
file that you uploaded to S3 in Step 7. - Click
Save
Step 11 – Set the Lambda function handler:
- In the Lambda function page, scroll down to the
Runtime settings
section and set the handler name tomain.handler
. - It will execute handler function under main.py file.
- Check Step-4 for reference.
Step 12 – Configure the Lambda function:
In the Lambda function page, navigate to the Configuration
section and under General configuration
set the following parameters:
- Memory (RAM): 1024 MB
- Memory (ROM): 512 MB
- Timeout: 1 minute
Click on Environment variables
Set any environment variables required by your FastAPI project
In the Lambda function page, scroll down to the Layers
> Add layer
.
- Select
custom layers
inlayer source
- Add the
requirements-package
to layer that you created in Step 9 - Choose
Version
and ClickAdd
Add Function URL
- In the Lambda function page, navigate to the
Configuration
section and underFunction URL
, ClickCreate function url
- Set
Auth type
asNone
. - Click Save
Step 13 – Test the Lambda function:
In the Lambda function page, Copy “Function Url” and test your function in any browser. just add.. <Function Url>/docs
in URL
You should see the output from your FastAPI project’s APIs.