Deploying a Python Flask Application to Azure App Service Using Azure CLI
Deploying a Python web application to the cloud can seem difficult when you are doing it for the first time. Azure App Service makes this process easier because it provides a managed environment for hosting web applications.
In this article, I will show how to deploy a simple Python Flask application to Azure App Service using Azure CLI. We will create the required Azure resources, package the application, and deploy it from the command line.
The example uses a small Flask application so that we can focus on the deployment process.
What We Need
For this example, we need:
An Azure account
Azure CLI installed on the computer
Python installed
Flask
A simple Flask application
A
requirements.txtfile
We will use Azure Resource Group, App Service Plan, and App Service to host the application.
Create a Simple Flask Application
First, create a folder for the application.
Inside the folder, create a file named app.py.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Azure!"
if __name__ == '__main__':
app.run(debug=True)
This is a simple Flask application.
The Flask class creates the application, and the @app.route('/') decorator defines what should happen when someone opens the application's home page.
When the root URL is opened, the application returns:
Hello from Azure!
Create requirements.txt
Next, create a file named requirements.txt.
Add Flask as a dependency:
Flask
The requirements.txt file tells the deployment environment which Python packages are required by the application.
The basic project structure should now look like this:
my-python-app/
│
├── app.py
└── requirements.txt
Step 1: Sign In to Azure
Open Command Prompt, PowerShell, or a terminal and run:
az login
This command opens the Azure sign-in process in the browser.
After signing in, the Azure CLI can be used to create and manage Azure resources from the terminal.
Step 2: Create a Resource Group
A resource group is a container for related Azure resources.
Create a resource group using the following command:
az group create --name my-quick-python-app-rg --location centralindia
Here:
--namespecifies the resource group name.--locationspecifies the Azure region.
You can choose an Azure region that is appropriate for your application and users.
Step 3: Create an App Service Plan
Next, create an App Service Plan.
az appservice plan create --name my-quick-python-app-plan --resource-group my-quick-python-app-rg --sku B1 --location centralindia --is-linux
The App Service Plan defines the computing resources used by the application.
In this example, the application uses a Linux App Service Plan.
The --sku B1 option selects the pricing tier used for the example. The appropriate tier should be selected according to the application's requirements and Azure pricing.
Step 4: Create the Web App
Now create the App Service web application.
The application name must be globally unique because it is used as part of the application's default Azure hostname.
az webapp create --name <your-unique-app-name> --resource-group my-quick-python-app-rg --plan my-quick-python-app-plan --runtime PYTHON|3.9
Replace <your-unique-app-name> with a unique name.
For example:
az webapp create --name my-python-flask-demo --resource-group my-quick-python-app-rg --plan my-quick-python-app-plan --runtime PYTHON|3.9
The Python runtime should be selected from the versions currently supported by Azure App Service. You can check the available Linux runtimes with:
az webapp list-runtimes --os-type linux
This is useful because supported runtime versions can change over time.
Step 5: Prepare the Application for Deployment
Before deploying the application, make sure both files are present:
app.py
requirements.txt
The application files need to be packaged into a ZIP file for the deployment method used in this example.
On Windows PowerShell, you can create a ZIP file with:
Compress-Archive -Path app.py, requirements.txt -DestinationPath my-app.zip
On Linux or macOS, you can use:
zip my-app.zip app.py requirements.txt
After this step, the project should contain:

Join the conversation! Your thoughts help the community grow.