Deploying Flask App - With Heroku

3/8/2021

Heroku is the simple and easiest way to deploy your backend APIs. The blog consists of step-by-step instructions to deploy your application along with a project template that makes it easy for you to organize your applications.

Note:: The following deployment process is followed by my PythonToProject Bootcamp students to deploy the API that they build over a 4 week period right from idea scoping to Design to development

Table of Contents

1. Create Heroku Account

If you are creating the account for the first time, choose Hobby Free Plan

2. Create New app

It will land you on the deploy page. We will come back to it in a little bit. We need a database before we host the app.

3. Setting up Heroku DB

This will create a DB and attach it to the application we created

4. Exploring DB Settings

After completion of the above step, you will land on the resources tab. If not, open the resources tab

Close the window and come back to the app window

4.1 Adding ENV Variables

  1. Go to the settings tab
  2. Click on reveal config vars
  3. You will see the DATABASE_URL key already added
  4. If you have any other env config use the same

5. Getting your Code Ready

db.init_app(app)
db.create_all()

Next, we need to ensure that our gunicorn server works fine with all the APIs. Not applicable for windows

6. Deploy the App - Method 1 - Using Heroku CLI

6.1 Install Heroku Cli

  1. Click on the deploy tab
  2. I hope you have installed Heroku-cli. If not time to install it - https://devcenter.heroku.com/articles/heroku-cli

6.2 Let's Deploy

heroku git:remote -a <your-herkou-app-name>
git add .
git commit -m <add a commit message>
git push heroku main

You will see Heroku building the app and Deploying the application. All of this happens seamlessly because we structured the application with a template that has

Once it is done, replace all your URLs with the Heroku Generated URL.

Redeploying application

git add <new_changes>
git commit -m "add new changes."
git push heroku main

7. Deploy the App - Method 2 - Using Github

7.1 Authorize Heroku App

Under the deploy tab, click on Connect to Github and click the button Connect to Github

From here on, any time you push your code to the main branch, the app will automatically deploy. You can manually deploy using Deploy from the Manual deploy section.

Checking logs

You can check the logs by clicking on the Heroku dashboard from More -> View Logs. If you use the Heroku-CLI method, check it using the following command

heroku logs --tail