DEV Community

loading...

How to deploy poetry based Fast-Api apps on Heroku

Aurel
a software developer who like challenges
・3 min read

Introduction

Fast-Api is a python framework which intended to help developers to build a modern API easily and quickly. It heavily relies on python standard typing module and Mypy for static type checking. Fast-Api is based on Starlette framework which is ASGI framework and allow to made use of python asyncio module.

Heroku is a Paas which help developers to easily deploy their app. Their offer a free plan which is enough to host an MVP or your hobby project. It can also be integrated to GitHub for automatic deployment on commit.

Poetry is a dependency and virtual env management program for python like Pipenv or many others.

The aims of this article is to show how you can deploy a Fast-Api project which use poetry as dependency management on Heroku. Many tutorials cover the deployment of Fast-Api on Heroku but fewer mention the particularity of poetry deployment on the Heroku platform.

Initializing the app

Create the app on Heroku

Creating the app with is a quite easy process. You just need to log in on https://www.heroku.com/ and create a new app. Make sure to select the language you are mainly using in your application.

Here we are focusing on Python.

Add the Procfile to your local project

Heroku read the instructions to start your app from a file named Procfile, located at the root of your project.

You need to set a web command in your Procfile which define how to start your app. For a Fast-Api project, it is basically:

web: uvicorn app.main:app --host=0.0.0.0 --port=${PORT}
Enter fullscreen mode Exit fullscreen mode

Poetry build pack for Heroku

To be able to install your Poetry dependencies, you need to add a poetry build pack to your Heroku app. The poetry build pack helps Heroku to figure out how to install those dependencies defined in the pyproject.toml as Heroku do not support Poetry by default as dependency manager.

If you already install the heroku-cli app, you could easily add the build pack by issuing the following instructions in your shell:

heroku buildpacks:clear
heroku buildpacks:add https://github.com/moneymeets/python-poetry-buildpack.git
heroku buildpacks:add heroku/python
Enter fullscreen mode Exit fullscreen mode

moneymeets/python-poetry-buildpack

The build pack help to create a requirement.txt file from your pyproject.lock during the build on Heroku.

Your Heroku app is now configured to run properly. Let me share with you this script that is useful for setting env var on Heroku. If you have a bunch of env vars to set up during your app creation on Heroku, then you will definitely need this script. It simply helps to set env var from a local .env.

I hope this post will helps you in a way.
You can follow me on Twitter if you like the content.

Related posts

How to Deploy Your FastAPI App on Heroku for Free

Autodeploy FastAPI App to Heroku via Git in these 5 Easy Steps

Discussion (0)

Forem Open with the Forem app