TLDR; Check it out on GitHub
FastAPI Serverless AWS API Template repo
Follow @JBarnden Star Watch Fork DownloadWhy Serverless?
I’ve been doing a lot of backend work with serverless recently and can’t recommend it highly enough! Here are a few reasons why:
- It’s cost effective compared to constantly running servers.
- Pay only for storage and the time your code runs.
- Readable infrastructure-as-code.
- Keeping your infrastructure changes in version control has a lot of benefits, like the ability to peer review or roll back if things go wrong.
- I personally find serverless a bit more reader friendly than something like Terraform for this purpose, but I’d still lean towards Terraform for managing other AWS services beyond Lambda
- You can use powerful plugins to simplify certain tasks (e.g. bundling python dependencies)
- It’s more environmentally friendly! 🍃
- If nobody is using your app, it doesn’t use any computing power.
Why FastAPI?
FastAPI is lightweight, customisable framework for building APIs. It’s highly extendable and can be used for simplistic setups, or more advanced setups with tools included for managing security, authentication and authorization. Because of its lightweight nature, it’s really fast to run, even with Lambda’s cold starts.
This Template
This is less of an intro to FastAPI, and more of a starting point for being able to deploy something that works quickly and get stuck in. There are a few other goodies that will come in handy if you choose to extend it. Here’s what’s in the box:
- A basic hello world API with a working serverless config
- Instructions for deploying and configuring multiple environments with their own environment variables
- CORS middleware for managing controlled browser based access to your API (you can read up on CORS here)
- Dockerised python dependencies in their own layer
- For compiling non-pure-Python modules, required for compiling on a Mac or Windows system and avoiding errors in Lambda.
- The ability to assign a custom domain to your API with an SSL certificate
- Thanks to the
serverless-domain-manager
plugin
- Thanks to the
- A logger that works both locally and when deployed to AWS Lambda
How it handles API requests
Instead of having multiple lambda functions for each of your API endpoints, we set up proxy resource, so API gateway sends all HTTP requests to a single Lambda function running our FastAPI code.
That request is then handled by a nifty package called Mangum, which we use as our handler to route our requests to the right functions.
Running locally
Set up a virtual python environment with a method of your choosing, then install requirements
pip install -r requirements.txt
Run your API locally with:
uvicorn main:app --reload
Once this is running, you should have access to your generated documentation at http://127.0.0.1:8000/docs
Deploying to AWS
- Run npm install from the root of the repository
- Set up AWS credentials (AWS profile or other means)
- Create a .env file from .env.example for your local environment
- Create an additional .env file for every environment you intend to deploy (e.g. .env.staging)
- Specify the desired domain for your API as the value of the BASE_URL environment variable (e.g. api.example.com)
- Create your certificate with Certificate Manager in the us-east-1 region and copy the ARN as the CERTIFICATE_ARN value
- Deploy with:
deploy --stage test
where test could be anything like staging or prod, whatever convention you prefer. - You’re done!
What next?
Good next steps in my opinion would be to:
- Dip into FastAPI tools for bigger multi-file APIs such as APIRouter
- Implementing security, authentication and authorization with FastAPI
I may make some follow up articles to cover these topics myself.
I hope you find this template useful, happy coding!