How to deploy poetry based Fast-Api apps on Heroku
Tags: python, heroku, fastapi, deployment
Published: 2021-04-21 17:16:14

## 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/](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:

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

## 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](https://devcenter.heroku.com/articles/using-the-cli) app, you could easily add the build pack by issuing the following instructions in your shell:

```bash
heroku buildpacks:clear
heroku buildpacks:add https://github.com/moneymeets/python-poetry-buildpack.git
heroku buildpacks:add heroku/python
```

[moneymeets/python-poetry-buildpack](https://github.com/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](https://github.com/sdkcodes/heroku-config) 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](twitter.com/aurelmegn) if you like the content.

## Related posts
[How to Deploy Your FastAPI App on Heroku for Free](https://towardsdatascience.com/how-to-deploy-your-fastapi-app-on-heroku-for-free-8d4271a4ab9)

[Autodeploy FastAPI App to Heroku via Git in these 5 Easy Steps](https://towardsdatascience.com/autodeploy-fastapi-app-to-heroku-via-git-in-these-5-easy-steps-8c7958ef5d41)