How to Deploy a FastAPI App on Heroku for Free
By: Shinichi Okada
Tags: Web Development, Programming, Fastapi, Free Hosting, Heroku

How to Deploy a FastAPI App on Heroku for Free

Image by mohamed_hassan from Pixabay

Introduction

Heroku is a cloud Platform As A Service (PaaS) supporting Ruby on Rails, Java, Node.js, Scala, Clojure, Python, PHP, and Go. Heroku’s free version offers 550–1000 dyno hours per month. Your apps sleep after 30 mins if no one is using your app. Your apps will be always free if your monthly dyno hours is within the limit.

The Dynos are isolated, virtualized Linux containers on the Heroku platform.

In this article, you will learn how easy it is to set up and run your FastAPI project and making it active 24/7 on Heroku.

I’m currently hosting this FastAPI website at Heroku as a demo.

Git

I use Gitstart to automate the Git initialization. From your terminal, it runs git init . Add .gitignore and README file and commit with a message. This creates a new repo in Github, and it will push all the files.

$ mkdir my_new_project

$ cd my_new_project

$ gitstart

Setting up Your Heroku Account

Personal accounts are given a base of 550 free dyno hours each month. In addition to these base hours, accounts that verify with a credit card [1] will receive an additional 450 hours added to the monthly free dyno quota. [2]

You need to add SSH Keys to Heroku.[3]

Install the Heroku CLI [4]

Then log in to Heroku from your terminal and create a Heroku app:

$ heroku login

$ heroku create

heroku create output. Image by Author.

You can add a custom domain to your Heroku app.[5]

How to Create the requirement.txt for FastAPI on Heroku

Heroku requires the requirement.txt to install your Python packages. pipreqs [6] generates it based on imports in your project.

$ pip install pipreqs

$ cd my_new_project

$ pipreqs ./

# if you already have the requirement.txt

$ pipreqs --force ./

Since pipreqs generates it based on import , you need to add the following to the requirements.txt manually.

uvicorn==0.12.3

aiofiles==0.6.0

python-multipart==0.0.5

jinja2==2.11.2

You need the uvicorn to start an ASGI server on Heroku. You need the aiofiles for your static files. If your project has a form, add the python-multipart . If your project uses a template engine, add the jinja2 .

runtime.txt

Heroku uses the default Python version. In order to use a certain Python version, add runtime.txt with your Python version. For example:

python-3.8.6

Procfile

Heroku requires the Procfile.[7]

If you have the main.py in the app directory, add the following:

web: uvicorn app.main:app --host=0.0.0.0 --port=${PORT:-5000}

If you have the main.py in the root directory:

web: uvicorn main:app --host=0.0.0.0 --port=${PORT:-5000}

Environment Variables

Heroku doesn’t use the .env file. Instead, add values in your .env file to Config Vars. You can find it under the Settings tab.

Adding environment variables to Config Vars. Image by Author.

How to Upload to Heroku

Now you are ready to push your project to Heroku:

$ git push heroku main

$ heroku open

The heroku open commands open your project in your browser.

git push heroku main on the terminal. Image by Author.

How to Add a Subdomain

I am using Cloudflare and this is what I had to do.

Select CNAME for the type. Add your subdomain name for the Name. Add your Heroku domain for the Content. Your Heroku domain name is something like, serene-example-4269.herokuapp.com.

The image below shows how to add toolbox.mywebsite.com subdomain.

Adding a subdomain to Cloudflare.

How to Keep Your App Active 24/7

After 30min of inactivity, the Heroku app goes idle. You can find your app process using heroic ps .

heroku ps outputs showing idle. Image by Author.

You can find the “idle” in the last line.

This is my other app process status showing “up”.

heroku ps outputs showing up. Image by Author.

cron-job.org [8] and Kaffeine [9] provide a free service. Both visit your URL at a scheduled time.

cron-job.org

You can create a cronjob by adding your URL and Execution schedule.

cron-job.org Cronjobs tab. Image by Author.

For your cronjob, you need to select Every 15 minutes. If you set it Every 30 minutes, it may delay 5 to 15 seconds.

Selecting Every 30 minutes for a cronjob. Image by Author.

You can edit or delete your cronjob at ease.

How to fix a failed cron-job

Your cron-job may fail telling “Response data too big” or “Output too large”.

Image by Author

The cron-job.org accepts up to 4 kB of response data from your URL/script. To fix it, you can create an endpoint that returns a JSON string rather than an HTML page.

Kaffeine

Kaffeine pings your Heroku app every 30 minutes so it will never go to sleep.

Kaffeine website.

How to remove your app from Kaffeine

The link in the website doesn’t work but if you click the link below it worked for me.

http://kaffeine.herokuapp.com/#decaf

Remove your app from Kaffeine.

How to Rename Your App

$ heroku apps:rename new_app_name

When you rename your app, don’t forget to update your cron job.

How to ssh to Heroku

You may want to ssh to Heroku. heroku ps:exec does that.

$ heroku ps:exec

Establishing credentials... done

Connecting to web.1 on ⬢ demo-desuyo...

~ $

Conclusion

Heroku provides max of 1000 hrs of free hosting for your project which uses under 512MB of RAM usage. You can use your domain name. If you require SSL on custom domains, you need to upgrade to the Hobby tier.

cron-job.org and Kaffeine provide a cronjob to visit your URL to avoid sleeping.

Newsletter

Get full access to every story on Medium by becoming a member.

References