Deploy a Flask REST API to Azure or AWS in Minutes

Tash-had Saqif
4 min readApr 28, 2020

TLDR; SSH into an Ubuntu VM and run this script. Remember to set a new Inbound Port Rule.

So, you want to deploy a Flask REST API? Okay — I’ll spare you the chiché web-tutorial lingo. Let’s get straight to it;

Step 1: Make a Flask REST API

Let’s start by building our Flask REST API. To keep things simple, we’ll be using a bare-bones API:

Code for a basic REST API

Make sure you have Flask installed, if you don’t be sure to do pip install FlaskYou can now run the app by doing python from your command line. When it’s running, I can go to through my web browser and it’ll return.

"message": "hello world"

Step 2: Setup a Virtual Machine


By now, you should have a Flask REST API that you can run locally. When you run the API with python, it runs your API as a process that is accessible through your local URL (ie. We can assign a publicly accessible URL to your locally running process, but this would be a bad idea. For example, if you put your computer sleep, your server will stop running. So, we can get a VM on Azure which is pretty much a computer that never turns off and stays dedicated to your process.

Head over to and hit “Create a Resource”. Select the latest “Ubuntu Server” — for me, this is “Ubuntu Server 18.04 LT” under the “Popular” section. Then go through all the “Project details” under the “Basics” tab. You can edit the other tabs if you’d like, I’m going to leave everything on defaults for simplicity. When you’re done, hit “Review + Create”.

Creating a virtual machine in Azure

When the VM deployment completes, you will have a running VM!


In this guide, we’re going to be moving forward with Azure. If you want to proceed using AWS, sign into the AWS Console, under “Services”, select “EC2”, click “Instances” on the lefthand pane, hit “Launch Instance”, and then “Select” the “Ubuntu Server”. It will take you to a screen to configure the instance before launching it. Click “Configure Security Group” on the top and hit “Add Rule”. Set the following config: Type=”Custom TCP Rule”, Protocol=”TCP”, Port Range=”5000", Source=”Custom:". Finally hit “Review and Launch”. Next, go back to “Instances” on the lefthand pane, press “Connect” on the top, and it will show you how to ssh into your EC2 instance. Then you can skip to Step 4.

Step 3: Open a Port for Inbound Traffic

Next, we need to open up a port to make our server publicly accessible. Note that in a production environment, this shouldn’t be done and you should restrict your traffic sources.

Navigate to “Home” (top left), hit “Virtual Machines” and select the VM you just created. Then hit “Networking” which is below “Settings” on the lefthand pane. You should see “Add Inbound Port Rule” on the left. Set the following configurations: Source=Any, Source port ranges=*, Destination=Any, Destination port ranges=5000, Protocol=Any, Action=Any. Hit “Add” when you’re done.

Allow Inbound Traffic to Your Virtual Machine

Return to the dashboard of your VM, and hit “Overview” on the lefthand pane. You should see a “Public IP address” with which you can access your VM. You should also see “DNS name” which allows you to set a custom label for your server (so you can use a domain to access it as opposed to an IP address). I’m going to set a custom DNS name with label “demo-api”. I can now ssh into my VM via

Step 4: Deploy Your Flask App

Now, we can deploy our Flask API to our VM. First, I need to put my code on GitHub. I’ll place my code in a repo I’ll call “my-app”. The repo will have two folders — “client” and “server”. Thus our repo structure will be as follows:

├── client/
└── server/

Before we SSH into the VM we created, take a minute to read through the README for the deploy script we’ll be using.

Next, ssh into your VM, clone the script, and cd into the folder.

git clone
cd azure-flask-deployment-script

Finally, run the script. For this demo, my script call will look like this:

sudo bash -s server tash-had my-app

Note that we used the optional -s argument since our server code is in a subdirectory named “server”. The remaining two arguments are my GitHub username and my repository name, respectively.

That’s it! You can now may requests to


where HOST is either your Public IP address or DNS Name (if you set a label), and PORT is the port which you deployed to (unless you used the -p flag, this will be 5000).