Check it out on GitHub

Follow @JBarnden Star Watch Fork Download

Overview

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

Running locally

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

via GIPHY

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!

Hi I'm James, I am a Software Engineer working at Amplyfi and I experiment with Machine Learning in my spare time. I'm mostly working with React at the moment, but I've also worked with Python, WordPress, PHP, C++, C# and Java.

Leave a Reply

avatar
  Subscribe  
Notify of