Deploy FastAPI to DigitalOcean App Platform
Previously I developed a python FastAPI tool that I developed locally to ingest Google ARI XML messages and produce a price quote here.
Now I want to publish this to get a deployed instance for public use
Using Digital Ocean to turn the above FastAPI into a deployed instance
Create a new project

Create an App

Grant GitHub access to repositories

Setup the repository to deploy

Choose options
In this case go with basic and make sure it as a web service

Skip Environment Variables
unless you want to use them

Setup info
keep the basics

Double check all your billing settings
And hit Create Resources

Important Caveats
Make sure you have separated your FastAPI routes and application into a separate file. I called mine app.py
That is put the routes and app
creation in that file like so
app = FastAPI()
@app.get("/ping", tags=["Test"])
async def ping():
return f'pong {pendulum.now().to_iso8601_string()}'
Then in your main.py
you can have something like this
from app import app
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)
To get this to run nicely on DigitalOcean create a Procfile
in the root folder that will run after container image is built. Contents of this
web: gunicorn --config gunicorn_config.py app:app
You will also need a gunicorn_confif.py
file as well with contents like this
bind = "0.0.0.0:8080"
workers = 2
worker_class = "uvicorn.workers.UvicornWorker"
worker_tmp_dir = "/dev/shm"
And then force a redeploy and you should be up and running.
[googlevr-calculator] [1] [INFO] Starting gunicorn 21.2.0
[googlevr-calculator] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[googlevr-calculator] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[googlevr-calculator] [14] [INFO] Booting worker with pid: 14
[googlevr-calculator] [15] [INFO] Booting worker with pid: 15
[googlevr-calculator] [14] [INFO] Started server process [14]
[googlevr-calculator] [14] [INFO] Waiting for application startup.
[googlevr-calculator] [14] [INFO] Application startup complete.
[googlevr-calculator] [15] [INFO] Started server process [15]
[googlevr-calculator] [15] [INFO] Waiting for application startup.
[googlevr-calculator] [15] [INFO] Application startup complete.