FastAPI python backend server with multi-stage CI/CD deployment
Last updated May 5, 2026
99
Stars
18
Forks
2
Issues
0
Stars/day
Attention Score
11
Language breakdown
Python 84.1%
Shell 11.2%
Dockerfile 3.3%
Makefile 1.4%
โธ Files
click to expand
README
FastAPI Serverless AWS Backend Server
This project act as a template for a FastAPI server deployed on AWS Lambda for production use.
Features
- Project structure template
- FastAPI boiler plate code:
.env file
- Logs are working also in production environment (AWS Lambda)
- Test suite setup with TestClient
- Health check route and exceptions routes
- Python code quality tools already setup both locally and as part of the CI (Pylint, Black, Isort, MyPy)
- Dockerfile with AWS lambda base image for the FastAPI service container
- Poetry for python environment management
- Serverless.yml boiler plate:
- Lambda function for the FastAPI backend service
- Lambda is container-based and images are deployed to AWS ECR
- Defined cloud watch IAM roles and retention policy (30 days)
- Defined XRay IAM roles
- Custom domain management support in three sub-domains, one per stage.
- Scripts folder so you can easily run it all
Future Features
- boiler plate code for caching using FastAPI_Cache
- Basic authentication and authorization with Auth0
- Database migration support using Alembic
Prerequisites
- An AWS account
- The AWS CLI installed and configured
- Python 3.7.2 or later
- Poetry for managing dependencies and packaging the app
- npm for installing the serverless framework and plugins
- serverless framework for deploying the backend (just run
npm install -g serverless) - Docker for building and testing the app locally
- (Optional) An AWS Certificate of SSL/TLS certificate for a domain name in the AWS Certificate Manager (this guide can help)
Getting Started
- Clone the repository
git clone https://github.com/larium/fastapi-serverless-aws-backend.git
- Install the dependencies
cd fastapi-serverless-aws-backend
npm install
poetry install
- Set up local environment in
.envfile - Create the file:
touch .env - Set the following values inside:
TBA
- Set access permissions to scripts directory
chmod 777 ./scripts/*
- Run local code analysis
./scripts/check
- Run local tests
./scripts/test
- Run local server
./scripts/start
Test it
curl --request GET "https://localhost:8000"
- (Optional) If you want to use a custom domain for the backend, follow these steps:
- Obtain an ARN of AWS Certificate for your domain in the AWS Certificate Manager
- Set it on the
.envfile inACM_ARN - In
serverless.yml-> setcustomDomain.enabled: true - Update the value of
baseDomainwith your base domain - Create the domain (this link a Route53 rule to the Certificate)
./scripts/create-domain
- Next time you will deploy the server will be exposed under your domain.
- Deploy to AWS
./scripts/deploy
Test it
curl --request GET "https://<UNIQUEID>.execute-api.<AWSREGION>.amazonaws.com/dev/"
- Remove deployment from AWS
./scripts/remove
- (Optional) Remove the domain from Route53 (it will not remove the certificate!)
./scripts/delete-domain
Environments
What | How (script) | Where (endpoint) --- | --- | --- Local |./scripts/start | http://localhost:8000
Remote|./scripts/deploy|https://*.execute-api.<AWS_REGION>.amazonaws.com/dev/
Develop Domain|./scripts/create-domain./scripts/deploy|https://api-develop.your.domain
Staging Domain|./scripts/create-domain staging./scripts/deploy staging|https://api-staging.your.domain
Production Domain|./scripts/create-domain prod./scripts/deploy prod|https://api.your.domain
Contributing
We welcome contributions to this project! If you have an idea for a new feature or improvement, please open an issue or pull request.License
This project is licensed under the MIT License. See the LICENSE file for details.Tips
Set python interpreter in VScode
- Run
poetry run which pythonand copy the python path command+shift+pto open the VScode command shortcuts and enterPython: Select Interpreter- Select
+ Enter interpreter path...and paste the python path - That's it.
๐ More in this category