Hosting FastAPI Service on Heroku

Hosting FastAPI Service on Heroku

We will host simple REST API built via FastAPI to Heroku

In this blog post, we will host a simple REST API via FastAPI python framework on Heroku which is more of a Platform as a Service(PaaS).

As per official documentation, below describes what FastAPI is all about

FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.6+ based on standard Python type hints.FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.6+ based on standard Python type hints.

Pre-requisites

  • Python 3.6+
  • IDE of your choice. (I prefer VSCode)
  • Heroku Account. (Just signup on Heroku website and also download/install Heroku CLI Tool for easy deployment as per your OS from here)

Local deployment of FastAPI application

Setup Python Virtual environment (recommended way to work with python projects), this will help to avoid any modules version ambiguity for future use.

  1. Create a project directory with a name of your choice and cd into that:
     mkdir fastapi-heroku
     cd fastapi-heroku
    
  2. Create Python Virtual Environment using the below command (virtualenv is the name of the environment, can be anything):
     python -m venv virtualenv
    
  3. Activate virtual environment by running activate script under ${projectDirectory}/Scripts. In the case of Windows OS, it is activate.bat
  4. Install FastAPI and uvicorn server dependency:
    pip install fastapi
    pip install uvicorn[standard]
    
  5. Create main.py with the following contents:

    from fastapi import FastAPI
    app = FastAPI()
    
    @app.get("/")
    async def root():
       return {"message": "Hello from Heroku via FastAPI"}
    
  6. Start FastAPI on uvicorn server using the below command:

    uvicorn main:app
    
  7. Application if no issues should start on the default port of 8000 image.png
  8. We will output all modules and associated dependencies required to run an application to requirements.txt so Heroku installs those as a part of starting up an application using the below command:
    pip freeze > requirements.txt
    

Hosting FastAPI on Heroku

  1. Heroku uses the concept of Procfile to know what additional commands or parameters are required to run the application.
  2. Create a file named "Procfile" and copy bellow contents to that in the same directory as requirements.txt and main.py
    web: uvicorn main:app --host=0.0.0.0 --port=${PORT:-5000}
    
  3. Now we are ready to push main.py, requirements.txt, and Procfile to deploy FastAPI to Heroku.
  4. Login to Heroku and create a new application using 'New --> Create new app' image.png
  5. Give app-name (my case have used "fastapi-heroku-deploy"), rest is self-explanatory and Click 'Create App'
  6. Now we will use Heroku CLI tool to push code. You can use any command-line tool, I will be using Git bash and follow instructions given by Heroku on the creation of the application as follows. Post git init step below you can create .gitignore file to avoid pushing of unwanted files like virtual environment directory, cache, etc. Sample .gitignore
  7. Go to the project directory and execute the below commands:
    heroku login
    git init
    heroku git:remote -a fastapi-heroku-deploy 
    #replace fastapi-heroku-deploy with your app name created in step 13.
    git add .
    git commit -am "Initial Commit"
    git push heroku master
    
  8. On git push heroku master Heroku CLI will try to identify application types like Python or Golang, deploy dependencies and then execute a command from Procfile to host application. Logs will show up as below on your local CLI tool too: deployment_logs.PNG
  9. Once all is successful it will show app URL generated to access the application as below (Basically has format <app-name>.herokuapp.com): image.png
  10. Voila! We have hosted FastAPI on Heroku and ready for public use.

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

Did you find this article valuable?

Support Virendra Oswal by becoming a sponsor. Any amount is appreciated!