Deploying a Simple Flask App on Azure VM

Michael Dacanay
3 min readJun 19, 2023

--

  1. Create a VM

Via the Azure portal:

Create VM resource

Configure subscription, resource group, VM name, etc. Make sure image is linux-based like Ubuntu (for ssh to work).

For this tutorial, I use password-based authentication. However, SSH is generally best practice.

Allow traffic from SSH, HTTP, and HTTPS.

For other options, leave at default. Create!

2. SSH to connect to VM shell (after the VM has been created, may take a few minutes)

Open up Terminal and type:
ssh azureuser@pip-second-vm.eastus.cloudapp.azure.com

If password-based authentication is used, the shell will prompt for password, then enter the password you configured. NOTE: if ssh complains about Remote Host Identification has changed, then ssh to IP address instead, ex: ssh azureuser@20.51.161.130. It can be found in VM overview page.

Virtual machine > Connect

3. Install Python/Flask on VM
Python 3.6 ( python3 ) is available on Ubuntu 18 by default. But pip package manager is not, so that is the first step.

# install pip3
sudo apt update
sudo apt install python3-pip

# check pip3 info, python3/pip3 are commands for the Python 3
# interpreter and package manager
pip3 --version
which python3
which pip3

# install Python package: Flask
pip3 install flask

The sudo apt update command is used to refresh the local package index on Ubuntu-based systems. It updates the package lists, which are repositories of available software packages and their versions. When you run sudo apt update, the system contacts the configured package repositories and checks for updates to the package lists. It downloads the latest package information, including metadata about available packages and their versions, but it does not install or upgrade any packages. This step is necessary before performing package installations or upgrades to ensure that you have the latest information about available packages on your system.

Sometimes, pip3 and python3 may be in different contexts. In that case, substitute references of pip3 with python3 -m pip e.g., python3 -m pip install flask.

4. Create simple Flask app and run with sudo permissions:
app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello world'

if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)

Port 80 is a privileged port (default port for HTTP), so run with sudo: sudo python3 app.py

5. Access via the browser

Public IP in browser, displays Flask app output

Use Public IP address or DNS name from VM overview page:

--

--

Michael Dacanay
Michael Dacanay

Written by Michael Dacanay

Intern at Tesla, Fidelity. Student at North Carolina State University

No responses yet