Check it out on GitHubFollow @JBarnden Star Watch Fork Download
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:
- Cost effective – pay only for storage and the time your code runs, when the code isn’t running, no charges are incurred.
- Infrastructure as code – issues are a lot easier to pinpoint when your infrastructure is documented through code.
- Highly flexible – tie individual functions to different endpoints, keep everything clean and separated.
FastAPI is something I’d heard some really good things about recently, I wanted a chance to dig into the very basics of it. From my initial dip into it, it looks very lightweight and highly customisable. It lends its self to anything from strait forward APIs to fully fledged APIs with authentication and access control.
This template 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. Here’s what’s in the box:
- A basic hello world API endpoint with a working serverless config
- The ability to assign a custom domain to your API through environment variables
Set up a virtual python environment with a method of your choosing, then install requirements
pip install -r requirements.txt
Locally deploy your API with
uvicorn main:app --reload
Once this is deployed, you should have access to generated swagger 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 for serverless (with AWS profile or other means)
- Create a .env file from .env.example
- Specify the desired domain for your API as the value of the BASE_URL environment variable
- Create your certificate with Certificate Manager in the appropriate region and copy the ARN as the CERTIFICATE_ARN value
- Deploy with:
deploy --stage test
where test could be anything like staging or live, whatever convention you prefer.
- You’re done!
I hope you find this template useful, happy coding!