Deploying FastAPI Applications on AWS Lambda: A Step-by-Step Guide
Written on
Chapter 1: Introduction to Serverless Architecture
In this initial segment, we will explore the setup of AWS SAM, the creation of a Lambda function, and the addition of FastAPI API endpoints. While many are familiar with serverless functions and their benefits, we won't delve into the specifics of what serverless functions are or their ideal use cases.
To summarize, a serverless architecture allows developers to concentrate on building applications without the hassle of managing servers. This can be likened to the concept of service apartments or paying guest accommodations—providing a secure environment with essential amenities and flexible customization options. For application development, opting for a serverless approach can help eliminate the complexities associated with server setup, security, and configuration.
Section 1.1: Understanding AWS Serverless Application Model (AWS SAM)
There are various methods to create a serverless Lambda function on AWS. You can use a Docker container with a lambda_handler and push it to AWS ECR, or compress the necessary files and upload them to AWS S3 to build a Lambda function via the console.
However, using AWS SAM is the most effective method for continuous integration and deployment. AWS SAM offers template specifications for defining serverless applications along with a CLI (command-line interface) tool for building and deploying. Additionally, it seamlessly integrates with GitHub Actions, allowing for a robust continuous development and deployment pipeline.
Requirements
To follow along with this guide, ensure that you have the following installed and configured:
- AWS CLI
- AWS SAM
- Visual Studio Code with the AWS Toolkit Extension
Section 1.2: Creating Your Function
- Open a new window in Visual Studio Code, and depending on your operating system, use either cmd+shift+p or ctrl+shift+p. Search for "AWS: Create Lambda SAM Application" and select it.
- Choose Python 3.7 or any preferred version of Python.
- Select the "AWS SAM Hello World" application template.
- Assign a name to your application, save it in a designated folder, and then export the folder to Visual Studio Code.
Once you add the folder to your workspace, the structure will resemble the one shown below:
Section 1.3: Adding Dependencies
To create APIs or endpoints using FastAPI, we first need to add FastAPI to the requirements.txt file:
# application_name/hello_world/requirements.txt
requests
fastapi
Subsection 1.3.1: Writing APIs with FastAPI
Next, let’s define some endpoints. Open the app.py file, comment out the lambda_handler function, and implement the endpoints as shown below.
Subsection 1.3.2: Setting Up the Handler with Mangum
To manage function invocations or various events through the API Gateway or Load Balancer, we use Mangum. Mangum is compatible with ASGI application frameworks such as Starlette, FastAPI, and Quart. For more information about Mangum, you can visit their documentation page.
Now, add Mangum to your requirements.txt:
# application_name/hello_world/requirements.txt
requests
fastapi
mangum
Then, create the lambda_handler in the app.py file.
Section 1.4: Updating the CloudFormation Template
To ensure access to all the endpoints created, we need to update the template.yaml file. Replace the content under Events with the following:
Events:
HelloWorld:
Type: Api
Properties:
Path: /{proxy+}
Method: any
Http:
Type: Api
Properties:
Path: "/"
Method: Any
After this update, your template file should appear similar to this:
Chapter 2: Building and Deploying Your Application
Now it's time to build and deploy your function. Open the terminal and navigate to the application directory.
Note: application_name refers to the name you assigned initially.
To build the application, use the following command:
sam build
Upon completion, a .aws-sam folder will be created, containing all necessary packages and files for running your application.
To deploy the application, execute:
sam deploy --region <your-region>
--stack-name <your-stack-name>
—resolve-s3 --capabilities CAPABILITY_IAM
For example, if you wish to deploy to the us-east-2 region and name the CloudFormation stack lambda-fastapi-deploy, your command would be:
sam deploy --region us-east-2
--stack-name lambda-fastapi-deploy
—resolve-s3 --capabilities CAPABILITY_IAM
After CloudFormation completes the deployment of your application to Lambda, you will receive the endpoint to access all APIs in the terminal.
In the first video titled "FastAPI AWS Lambda Deployment with GitHub Actions Pipeline," you'll find a detailed overview of deploying FastAPI applications using AWS Lambda and GitHub Actions.
The second video, "Deploying to AWS Lambda in Python with Serverless Framework + GitHub Actions (Modern CI/CD)," provides insights into deploying Python applications to AWS Lambda using the Serverless Framework and modern CI/CD practices.
Conclusion
This guide primarily focused on the discovery and setup processes for creating APIs with FastAPI, integrating them with Mangum, and deploying them as a Lambda function using a CloudFormation template. In the next installment, we will explore creating tables within DynamoDB and accessing them using the Lambda function established here.