Auto-deploy FastAPI App to Heroku via Git in these 5 Easy Steps | by Mandy Gu | Towards Data Science

If you are not working with an existing app, you can refer to my Iris Classifier FastAPI App for reference. I wrote an article about how to set that up (the Docker component is optional).
Note: I put the Iris app together really quickly to demonstrate how to set up FastAPI — it certainly does not adhere to best practices when it comes to serving model predictions
towardsdatascience.com
This is my GitHub repo for the Iris Classifier app. We will be using this as an example in this tutorial. Make sure that the FastAPI project you are working with is also hosted as a GitHub repository.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
The FastAPI website provides more detail on how FastAPI works and the advantages of using FastAPI.
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.
I really like using Heroku to showcase my side projects because it’s easy to set up and the free tier is usually enough for most purposes.
In this article, I’m going to use Heroku to deploy my FastAPI app to the public internet and sync the master branch with the production app. Any time I push changes to the master branch, they will sync on the web app.
The Procfile specifies the commands that are executed by the app upon startup. Create a new file called Procfile (no extensions, make sure you are using the same casing).
This is the Procfile for the Iris classifier:
web: uvicorn iris.app:app --host=0.0.0.0 --port=${PORT:-5000}
If you are using your own FastAPI project, change the path iris.app:app accordingly. This is the path to the instantiated FastAPI() object.
Here is mine for reference.
Follow the instructions on the Heroku website to download the CLI:
devcenter.heroku.com
Although we aren’t actually using the CLI at all here, the CLI makes it easy to manage the app through the terminal.
Go to https://www.heroku.com/ and create an account. Navigate to the dashboard to create a new app.
I named my app irisclassifierfastapi. The name determines the default URL — this project will be hosted at www.irisclassifierfastapi.herokuapp.com. Custom domains can be added to the app on the app’sSettings page.
Navigate to the deploy tab and click on the GitHub logo to authenticate and authorize Heroku to deploy code from your GitHub account.
Once connected, select the repository to connect to the app. From there, choose a branch and hit Enable Automatic Deploys. If you are using a CI tool, you can check the checkbox to ensure that CI passes before changes are deployed.
The next time you push changes to your master branch, the app will be deployed. Since we haven’t deployed our app yet (and have made no changes so far), we can trigger a manual deployment by scrolling to the bottom of the deployment tab.
If you installed the CLI, manual deploys can also be executed through the command line.
devcenter.heroku.com
It takes close to a minute for changes to be deployed. You can find the Swagger UI for the Iris Classifier on https://irisclassifierfastapi.herokuapp.com/docs
As long as my application is running, this CURL request allows me to fetch predictions from the classifier.
curl 'http://irisclassifierfastapi.herokuapp.com/iris/classify_iris' -X POST -H 'Content-Type: application/json' -d '{"sepal_l": 5, "sepal_w": 2, "petal_l": 3, "petal_w": 4}'
The classifier predicts that this iris is a Virginica with 91% probability.
Here are some tools you can leverage to debug bad deployments and to monitor your app in general.
In the app page, click More and select View logs from the drop down menu to see application logs. Each log is an event that occurred within the app. Logs can be used to provide information (for instance, you can log user attributes), warnings and errors.
You can use the Python logging library to add more logs that will make it easier to debug your application.
In case of a bad deployment, you may want to rollback your changes. While you can do this in Git by reverting to a previous commit, Heroku also provides the option of doing it through the UI.
Navigate to the Activity tab to see previous builds and click Roll back to here to revert to a previous build.
Metrics are not available for the free tier. The metrics charts capture information related to system health and performance, such as memory usage and event occurrence.
This is what the metrics tab look like for one of my apps under the Hobby tier:
If you are interested in learning more about app development, check out this course.
click.linksynergy.com
If you enjoyed this article, consider buying me a coffee — every small contribution helps me find more time to work on this blog. Follow me on Medium for the latest updates. 😃
I am also building a comprehensive set of free Data Science lessons and practice problems at www.dscrashcourse.com as a hobby project.
Thank you again for reading! 📕