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
- 2. Create New app
- 3. Setting up Heroku DB
- 4. Exploring DB Settings
- 4.1 Adding ENV Variables
- 6. Deploy the App - Method 1 - Using Heroku CLI
- 7. Deploy the App - Method 2 - Using Github
- Checking logs
1. Create Heroku Account
- Go to heroku.com
- Signup and create an account
- Login to the account
If you are creating the account for the first time, choose Hobby Free Plan
2. Create New app
- Once you are in the dashboard, click on the New button & then
Create New App.
- Give an app name, choose a region, ignore the pipeline
- Click Create
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
- Click on the resources tab
- On the bottom next to the search bar, you will see
Find more addons.
- Click on it, and it will redirect to the add-ons page
- To find Postgres do a
Ctrl+Fand search for Postgres
- Click on Heroku Postgres
- On the next page, click
Install Postgreson the top right corner
Hobby planand search for your app name in the search bar
submit order form.
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
- Click to open the new Postgres resource. It might take a few seconds to load
- Click on the settings tab. It will expose the Postgres connection URL
Close the window and come back to the app window
4.1 Adding ENV Variables
- Go to the settings tab
- Click on reveal config vars
- You will see the
DATABASE_URLkey already added
- If you have any other env config use the same
5. Getting your Code Ready
- Clone the project template - github.com/thelearningdev/python-project-te..
- Add your flask models to
- Add your REST APIs to
- Ensure your
models.pyhas the following snippet at the end. Uncommented.
- Ensure your app runs without any error
python run.py. Test all your APIs. Once it runs fine, stop the application with
ctrl + C.
Next, we need to ensure that our gunicorn server works fine with all the APIs. Not applicable for windows
- Run the app with gunicorn
gunicorn run:app -b :5000
- Check one of your APIs
- Stop the app
6. Deploy the App - Method 1 - Using Heroku CLI
6.1 Install Heroku Cli
- Click on the deploy tab
- I hope you have installed Heroku-cli. If not time to install it - devcenter.heroku.com/articles/heroku-cli
6.2 Let's Deploy
- On the terminal, run
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.
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
- On the screen that pops up
Authorize Herokuto provide access to your Github account
- Search for the repository and click connect
- Click on
Enable Automatic deploys.
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.
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