Part 5 — Tutorial: PaaS with Heroku
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”.
Overview of the steps
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 gunicornSection 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.txtrequirements.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:appRead 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.
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 masterThe 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.
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.
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 masterHeroku rebuilds and redeploys automatically. Refresh the browser to see the new version.
Section point: Other PaaS platforms, such as Google App Engine, follow a similar process.
