A project template for deploying FastAPI applications to AWS Lambda using the AWS Serverless Application Model (SAM) framework, providing a complete solution for building serverless APIs.
- High-performance REST API built with FastAPI
- Seamless integration with AWS Lambda using the Mangum adapter
- Container-based Lambda deployment using Docker
- Automatic API documentation generation
- Simplified deployment and infrastructure management with AWS SAM
- Support for local development and testing
Before getting started, ensure you have the following tools installed:
- Python 3.11
- Docker
- AWS CLI
- AWS SAM CLI
- AWS account with configured credentials
fastapi-lambda
├── .aws-sam/ # SAM build folder (auto-generated)
├── .venv/ # Virtual environment directory
├── app/ # Application code directory
│ ├── __init__.py
│ └── main.py # FastAPI main application
├── docs/ # Documentation files
│ ├── infra.dot
│ └── infra.svg
├── Dockerfile # Docker container configuration
├── README.md # This document
├── requirements.txt # Python dependencies
├── samconfig.toml # SAM configuration file
└── template.yaml # SAM template file
After cloning the repository:
# Navigate to project directory (if not already there)
cd fastapi-lambda
# Build the project
sam buildBefore deploying, you need to create an ECR repository to store your container image:
aws ecr create-repository --repository-name fastapi-lambda --region us-east-1Take note of the repository URI in the output (it will look like: account-id.dkr.ecr.us-east-1.amazonaws.com/fastapi-lambda).
sam deploy --image-repository account-id.dkr.ecr.us-east-1.amazonaws.com/fastapi-lambdaAfter deployment completes, you can view the deployed resources and API Gateway URL:
aws cloudformation describe-stacks --stack-name fastapi-lambda --query "Stacks[0].Outputs"Look for the ApiGatewayEndpoint value in the output - this is the URL of your deployed API.
This README provides a comprehensive guide to get started with your FastAPI on AWS Lambda project. Customize it to match your specific project requirements and preferences.