TLDR; Check it out on GitHub
Follow @JBarnden Star Watch Fork DownloadI’ve been using the serverless.io framework to deploy low-cost serverless APIs to AWS, but recently discovered they’re now requiring a subscription to continue using it. Popular plugins are also falling behind on their maintenance, so I’ve been in search of an alternative
Terraform
I’ve chosen Terraform because it’s been around for so long, it’s robust and has a thriving community. While it might not be quite as simplistic as the serverless.yml we produced in my tutorial on Serverless FastAPI Lambda, it’s still pretty lightweight and offers a few benefits:
- With the AWS provider we can define any AWS services we want, not just limited to Lambda and API gateway
- While this was possible with serverless.io too, it relied on writing cloudformation templates inside your yml
- We’re not limited to AWS, we can also lean on providers for Google Cloud Platform and Azure.
- Their community edition is free at any scale (at time of writing)
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
Serves as a starting point for a quickly deployable API, with the option to configure for multiple deployment environments (e.g. dev, staging, prod). What’s in the box:
- A basic “Hello World” app that tests environment variable and logging configuration when you call the
/hello/endpoint - CORS middleware for managing controlled browser based access to your API (you can read up on CORS here)
- Quickstart instructions for environment configuration and deployment
Terraform code – what it do?
In short, the terraform code will:
- Package the FastAPI code in the `code` directory into an image whenever a file change is detected
- Deploy the image to an environment specific ECR repository
- Create or update a Lambda function (when needed) that uses the latest image pushed to that ECR repository
- Clean up older images in the ECR repository based on the configurable, per-environment lifecycle policy
If you want to destroy test resources..
You can use terraform destroy -var-file="development.tfvars"
How it handles API requests
Instead of having multiple lambda functions for each of your API endpoints, all incoming requests to your single function are routed to your defined FastAPI endpoints by Mangum, which we use as our handler to route our requests to the right functions.
Running locally
To run a local version with hot reloading you can use the included docker compose file, from the root:
- Copy the example env file with
cp .env.development.example code/.env.development docker compose up --build
You should now see your swagger docs at: http://localhost:8000/docs
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!

