SECTION 06 / 9

Part 5 — Tutorial: PaaS with Heroku

3 min read
On this page 6 headings

Heroku is a PaaS provider: it provides the entire platform (the infrastructure and an easy way to deploy). The tutorial deploys a very simple Flask application with one route that displays “Hello World”.

Running locally vs deployed on Heroku: locally, Flask’s development server runs the app at http://127.0.0.1:5000 for the developer only; after deployment, Heroku runs it on a cloud platform accessible over the internet.
Running locally vs deployed on Heroku: locally, Flask’s development server runs the app at http://127.0.0.1:5000 for the developer only; after deployment, Heroku runs it on a cloud platform accessible over the internet.
Open image ↗
Illustration: Part 5 — Tutorial: PaaS with Heroku

Overview of the steps

Deploying to Heroku: (1) create the Flask app, (2) prepare the files — requirements.txt and Procfile, (3) create the Heroku app, (4) push the code with git, which builds and deploys it, (5) the live web app is accessible to users.
Deploying to Heroku: (1) create the Flask app, (2) prepare the files — requirements.txt and Procfile, (3) create the Heroku app, (4) push the code with git, which builds and deploys it, (5) the live web app is accessible to users.
Open image ↗
Illustration: Part 5 — Tutorial: PaaS with Heroku

Step 1 — Prepare the application

(a) Create a virtual environment. This keeps the project’s packages separate, so they do not conflict with other packages on your system. Activate it. Running pip freeze at this point shows nothing installed.

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip freeze                 # shows nothing yet

(b) Install the needed modules: Flask and Gunicorn.

pip install flask gunicorn

Section point: Gunicorn is a Web Server Gateway Interface (WSGI) server. Heroku uses it to run Python web applications.

Installing these also pulls in some other packages they depend on. pip freeze now lists all the packages with their versions.

(c) Save the dependency list in requirements.txt.

pip freeze > requirements.txt

requirements.txt is the list of packages (with version numbers) the project needs. Heroku reads it and installs exactly those packages and versions.

(d) Create the Procfile. The Procfile tells Heroku how to run the application. For this app it contains one line:

web: gunicorn app:app

Read it as: “this is a web application; run it with the gunicorn server; in the file app.py there is a variable called app (the Flask object), and that is what must be run.”

Section point: requirements.txt (what to install) and the Procfile (how to run) are the two important settings you must prepare for Heroku.

The two Heroku settings: requirements.txt lists the Python dependencies (Flask, gunicorn) — what to install; the Procfile (web: gunicorn app:app) is the startup command — how to run the app. Heroku installs the packages and starts the web process.
The two Heroku settings: requirements.txt lists the Python dependencies (Flask, gunicorn) — what to install; the Procfile (web: gunicorn app:app) is the startup command — how to run the app. Heroku installs the packages and starts the web process.
Open image ↗
Illustration: Part 5 — Tutorial: PaaS with Heroku

Step 2 — Create an app on Heroku

  • Sign up or log in on the Heroku website.
  • Click New → Create new app, give it a name and choose a region (the example chose Europe).
  • Heroku’s documentation on running Python apps (installing dependencies, the Procfile) is available online and was used to set up the app.

Step 3 — Deploy using the Heroku CLI and Git

Heroku offers several ways to deploy. The tutorial uses the Heroku command-line interface (CLI):

heroku login                      # opens a browser to log in
git init                          # start a git repository
heroku git:remote -a <app-name>   # add remote "heroku"
git add Procfile app.py requirements.txt
git commit -m "initial commit"
git push heroku master

The key idea is that Heroku is the git remote. Instead of pushing to GitHub, you push your master branch to Heroku. Only the project files are added; the virtual-environment folder is not.

Deploying to Heroku using Git: prepare the Flask app, create an app through New → Create new app (Europe), connect Git with heroku git:remote -a <app-name>, commit, then run git push heroku master. Heroku builds and deploys the application.
Deploying to Heroku using Git: prepare the Flask app, create an app through New → Create new app (Europe), connect Git with heroku git:remote -a <app-name>, commit, then run git push heroku master. Heroku builds and deploys the application.
Open image ↗
Five steps for deploying a Flask Hello World application to Heroku using the master branch.

Pushing does two things: it uploads the code, and it starts the deployment. In the build output you can watch Heroku:

  • install Python and pip,
  • install everything listed in requirements.txt,
  • read the Procfile, understand that this is a web application, and launch it.

Open the given URL in a browser and the app (“Hello World”) is live.

Step 4 — The Heroku dashboard

On the Heroku website you can see that the build succeeded and the app is deployed. Heroku also provides:

  • Metrics: in the lecture these required adding a credit card.
  • Resources / add-ons: data stores, caching, content management and more.
The Heroku dashboard: application overview (status Running), controls (Overview, Resources, Settings, Activity), and status checks — web process, recent release and logs.
The Heroku dashboard: application overview (status Running), controls (Overview, Resources, Settings, Activity), and status checks — web process, recent release and logs.
Open image ↗
Illustration: Part 5 — Tutorial: PaaS with Heroku

Step 5 — Deploying an update

Updating is just the normal git commit process. For example, change the message to “Hello World new version”, then:

git status
git add app.py
git commit -m "change the message"
git push heroku master

Heroku rebuilds and redeploys automatically. Refresh the browser to see the new version.

Updating a Heroku app: (1) change the code, (2) commit and push to the Heroku Git remote, (3) Heroku builds, releases and runs the new version, (4) check the live application to verify the update.
Updating a Heroku app: (1) change the code, (2) commit and push to the Heroku Git remote, (3) Heroku builds, releases and runs the new version, (4) check the live application to verify the update.
Open image ↗
Illustration: Part 5 — Tutorial: PaaS with Heroku

Section point: Other PaaS platforms, such as Google App Engine, follow a similar process.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑