This project act as a template for a FastAPI server deployed on AWS Lambda for production use.
- Project structure template
- FastAPI boiler plate code:
- Root application with versioning API (currently V1 and V2 are defined)
- Middleware for CORS, exceptions and logging
- Config module with settings loaded from your
.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
- boiler plate code for caching using FastAPI_Cache
- Basic authentication and authorization with Auth0
- Database migration support using Alembic
- 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)
- 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
.env
file
- 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
.env
file inACM_ARN
- In
serverless.yml
-> setcustomDomain.enabled: true
- Update the value of
baseDomain
with 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://<UNIQUE_ID>.execute-api.<AWS_REGION>.amazonaws.com/dev/"
- Remove deployment from AWS
./scripts/remove
- (Optional) Remove the domain from Route53 (it will not remove the certificate!)
./scripts/delete-domain
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 |
We welcome contributions to this project! If you have an idea for a new feature or improvement, please open an issue or pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Run
poetry run which python
and copy the python path command+shift+p
to open the VScode command shortcuts and enterPython: Select Interpreter
- Select
+ Enter interpreter path...
and paste the python path - That's it.