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:
my-python-app/
│
├── app.py
├── requirements.txt
└── my-app.zip
Step 6: Deploy the Application
Now deploy the ZIP file to Azure App Service.
az webapp deployment source config-zip --resource-group my-quick-python-app-rg --name <your-unique-app-name> --src my-app.zip
Replace <your-unique-app-name> with the name of the web app created earlier.
The Azure CLI uploads the ZIP file and deploys the application to the App Service.
Step 7: Open the Application
After the deployment is completed, the application can be opened using the default App Service URL.
The URL normally follows this format:
https://<your-unique-app-name>.azurewebsites.net
Open the URL in a browser.
If the deployment and application configuration are correct, you should see:
Hello from Azure!
This confirms that the Flask application has been deployed to Azure App Service.
How the Deployment Works
The deployment process consists of several Azure resources and commands.
Step | Azure Component | Purpose |
|---|---|---|
1 | Azure CLI | Connects to and manages Azure |
2 | Resource Group | Organizes Azure resources |
3 | App Service Plan | Provides the hosting resources |
4 | App Service | Hosts the Python web application |
5 | ZIP deployment | Uploads the application files |
6 | App Service URL | Provides access to the deployed application |
The main benefit of using Azure CLI is that the deployment can be performed directly from the terminal without manually creating each resource through the Azure portal.
Common Problems
Python Runtime Is Not Available
If Azure reports that the selected Python runtime is not available, check the currently supported runtimes:
az webapp list-runtimes --os-type linux
Then use a supported Python runtime in the az webapp create command.
Application Does Not Start
Check that app.py and requirements.txt are included in the deployment package.
Also make sure that Flask is listed in requirements.txt.
Deployment Fails
Check the Azure CLI output for the error message. You can also check the application's deployment and application logs through Azure App Service.
Application Shows an Error
Make sure the Flask application has the expected entry point and that the required dependencies are installed.
For a real application, additional App Service configuration may also be required depending on the framework and application structure.
Best Practices
A few things are important when moving from a simple example to a real application:
Use a supported Python version.
Keep dependencies in
requirements.txt.Do not store passwords, API keys, or other secrets directly in the source code.
Use application settings for configuration values.
Enable HTTPS for applications handling sensitive information.
Check application logs when troubleshooting deployment problems.
Choose the App Service pricing tier based on the application's actual requirements.
Test the application locally before deploying it to Azure.
Advantages
Using Azure CLI with Azure App Service provides several benefits:
Deployment can be performed from the command line.
Azure resources can be created using repeatable commands.
App Service manages the underlying hosting infrastructure.
Python applications can be hosted without managing virtual machines directly.
The same deployment approach can be used as part of a larger development workflow.
Disadvantages
There are also some limitations to consider:
Azure App Service has an associated cost depending on the selected plan.
More complex applications may require additional configuration.
The available runtime versions can change over time.
Production applications usually need additional configuration for security, monitoring, scaling, and application settings.
Conclusion
Deploying a Python Flask application to Azure App Service does not require a large amount of configuration for a simple application. With Azure CLI, we can create the required Azure resources, package the application, and deploy it from the command line.
In this example, we created a Resource Group, created an App Service Plan, created a Python web application, packaged the Flask application, and deployed it using ZIP deployment.
For a production application, additional configuration such as application settings, authentication, monitoring, logging, security, and scaling should be considered. However, this example provides a simple starting point for developers who want to understand how Python applications can be deployed to Azure App Service using Azure CLI.

Join the conversation! Your thoughts help the community grow.