/fly-fastapi

Deploying a simple FastAPI app to Fly.io >> https://fly-fastapi.fly.dev/docs <<

Primary LanguageMakefile

Fly FastAPI

>> Deploying a simple FastAPI app to Fly.io <<

 

Fly.io is a neat service that allows you to quickly deploy your application in the cloud. I wanted to take it for a spin and deploy a small Python app for starters. By default, it supports deploying apps built with frameworks like Express, Nuxt, Django, Rails, etc. However, I wanted to see if I can make it work with a small app built with FastAPI and serve it via Uvicorn-Gunicorn combo.

Turns out that if you can Dockerize your app, you can deploy it with Fly.io regardless of the stack. Also, the deployment is completely automatic here; it's done by GitHub Action and the deployment only kicks in if the corresponding commit has a tag that starts with v*.

Check out the deployed app

The deployed app is no longer accessible. The service got DDOSed like crazy!!!

  • To access the deployed OpenAPI docs provided by FastAPI, go to https://fly-fastapi.fly.dev/docs.

  • Use the doc to make requests to the GET /greetings API endpoint. This endpoint leverages HTTP basic auth. The username is ubuntu and the password is debian.

  • Or, you can use cURL to test the endpoint:

    curl -X GET https://fly-fastapi.fly.dev/greetings -u ubuntu:debian

    This will return:

    {
      "ok": true,
      "message": "Hello from Fly!"
    }

If you can't access the app, that probably means, I tore down the container to save money; you can still test it out locally.

Run the app locally

Make sure you have the latest version of Docker and docker-compose (v2) installed.

Run the unit tests

  • Create and activate a virtual environment.

  • Install the dependencies, run:

    pip install -r requirements.txt -r requirements-dev.txt
  • Run the tests:

    pytest -s -v

    This will return:

    tests/test_main.py::test_auth_error PASSED
    tests/test_main.py::test_ok PASSED
    
    ============================ 2 passed in 0.16s =============================
    

Todo

  • Add Cloudflare caching and DDOS protection.
  • Deploy via GitHub action. Currently, this is deployed via flyctl.

Resources

✨ 🍰 ✨