This project provides a minimal Python Flask web application that can be deployed to Azure App Service. It includes automated scripts for dependency installation, Azure resource creation, and deployment.
For experienced users, here's the quick start process:
- Install Python 3.11 and PowerShell 7
- Run
InstallDependencies.bat - Copy
env-file-template.txtto.envand configure your Azure settings - Run
CreateAzureWebApp.batto create Azure resources - Run
DeployAzureWebApp.batto deploy your application - Visit your web app URL to verify deployment
That's it! Your Flask application should now be running on Azure App Service.
- Project Overview
- Prerequisites
- Required Packages
- Installation Guide
- Environment Configuration
- Creating the Azure Web App
- Deploying the Web App
- Additional Resources
- Simple Flask web application with multiple endpoints
- Automated dependency installation
- Automated Azure Web App creation
- Automated deployment to Azure
- Environment-based configuration
- Cross-platform PowerShell scripts
Minimal-Azure-WebApp-Python/
├── .vscode/ # VS Code configuration
│ ├── launch.json # Debug configuration for Flask
│ └── tasks.json # Task to open browser automatically
├── src/ # Application source code
│ ├── app.py # Main Flask application
│ └── requirements.txt # Python dependencies
├── .env # Environment configuration (you create this)
├── env-file-template.txt # Template for .env file
├── InstallDependencies.bat # Install Python dependencies
├── CreateAzureWebApp.bat # Create Azure resources (Windows)
├── CreateAzureWebApp.ps1 # Create Azure resources (PowerShell)
├── DeployAzureWebApp.bat # Deploy application (Windows)
├── DeployAzureWebApp.ps1 # Deploy application (PowerShell)
├── .gitignore # Git ignore rules
├── LICENSE # Project license
├── README.md # This documentation
└── assets/ # Endpoint screenshots
├── WebApp-Root.jpg
├── WebApp-Test.jpg
├── WebApp-Wikipedia.jpg
└── WebApp-Time.jpg
Before you begin, ensure you have the following installed on your system:
-
Python 3.11 or higher
- Download from: https://www.python.org/downloads/
- Ensure Python is added to your system PATH
-
PowerShell 7 (required for automation scripts)
- Download from: https://github.com/PowerShell/PowerShell/releases
- Install to default location:
C:\Program Files\PowerShell\7\pwsh.exe
-
Azure CLI (automatically installed by scripts if missing)
- Manual installation: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
-
Azure PowerShell Module (automatically installed by scripts if missing)
- Manual installation:
Install-Module -Name Az -Scope CurrentUser
- Manual installation:
- Azure Subscription with appropriate permissions
- Resource Group creation permissions
- App Service creation permissions
The application uses the following Python packages as defined in src/requirements.txt:
Flask==3.0.0
gunicorn==23.0.0
requests==2.31.0- Purpose: Web framework for creating the web application
- Features Used:
- Route handling (
@app.route) - Request handling (
request)
- Route handling (
- Purpose: WSGI HTTP Server for serving the Flask application in production
- Usage: Azure App Service uses Gunicorn to serve the Flask application
- Configuration: Automatically configured by Azure App Service
- Purpose: HTTP library for making HTTP requests to external APIs
- Features Used:
- HTTP GET/POST requests
- JSON response handling
- API integration
The project includes an automated script to install all required Python packages.
- Navigate to the project root directory
- Double-click
InstallDependencies.bator run from command prompt:InstallDependencies.bat
@echo This will install all dependencies listed in ./src/requirements.txt
pause
@echo off
rem change to folder where BAT file is
cd /d "%~dp0"
pip install -r ./src/requirements.txt
pauseIf you prefer to install dependencies manually:
cd /d "<YOUR_LOCAL_PROJECT_PATH>"
pip install -r ./src/requirements.txtThe project uses environment variables for Azure configuration. You need to create a .env file based on the provided template.
-
Copy the template file:
- Source:
env-file-template.txt - Destination:
.env(in project root)
- Source:
-
Template contents:
# Azure Deployment AZURE_SUBSCRIPTION_ID=cccccccc-cccc-cccc-cccc-cccccccccccc AZURE_TENANT_ID=dddddddd-dddd-dddd-dddd-dddddddddddd AZURE_RESOURCE_GROUP=my-resourcegroup-2342934563 AZURE_LOCATION=swedencentral AZURE_APP_NAME=my-webapp-934238764295 AZURE_PYTHON_VERSION=3.11 AZURE_APP_SERVICE_PLAN=appservice-plan
Replace the template values with your actual Azure configuration:
| Variable | Description | Example | Required |
|---|---|---|---|
AZURE_SUBSCRIPTION_ID |
Your Azure subscription ID | 12345678-1234-1234-1234-123456789012 |
Yes |
AZURE_TENANT_ID |
Your Azure tenant ID | 87654321-4321-4321-4321-210987654321 |
Yes |
AZURE_RESOURCE_GROUP |
Resource group name (will be created if doesn't exist) | my-python-webapp-rg |
Yes |
AZURE_LOCATION |
Azure region for deployment | swedencentral, eastus, westeurope |
Yes |
AZURE_APP_NAME |
Unique name for your web app | my-unique-webapp-12345 |
Yes |
AZURE_PYTHON_VERSION |
Python runtime version | 3.11, 3.10, 3.9 |
Yes |
AZURE_APP_SERVICE_PLAN |
App Service Plan name | my-app-service-plan |
Yes |
To find your Azure Subscription ID and Tenant ID:
-
Using Azure Portal:
- Go to https://portal.azure.com
- Navigate to "Subscriptions"
- Copy the Subscription ID
- Navigate to "Microsoft Entra ID"
- Copy the Tenant ID
-
Using Azure CLI:
az account list --output table az account show --query "tenantId"
Common Azure locations:
swedencentral(Sweden Central)eastus(East US)westeurope(West Europe)
Your AZURE_APP_NAME must be globally unique across all Azure App Services.
Example: mycompany-webapp-20240614-001
The project includes automated scripts to create all necessary Azure resources for your web app.
- Ensure .env file is configured (see Environment Configuration section)
- Ensure you have Azure permissions to create resources
- Have your Azure credentials ready for authentication
-
Double-click
CreateAzureWebApp.bator run from command prompt:CreateAzureWebApp.bat
-
Follow the prompts:
- The script will pause for confirmation before proceeding
- Press any key to continue when prompted
-
What it does:
- Checks if PowerShell 7 is installed
- Unblocks all *.ps1 files
- Runs CreateAzureWebApp.ps1 with PowerShell 7
The PowerShell script performs the following actions:
- Reads and validates the
.envfile - Sets default values for optional parameters:
$config.OS = "Linux"
$config.AZURE_APP_SERVICE_PLAN_TIER = "Basic"
$config.AZURE_APP_SERVICE_PLAN_SIZE = "Small"
$config.AZURE_APP_SERVICE_PLAN_WORKER_COUNT = 1
- Azure PowerShell Module: Installs if not present
- Azure CLI: Downloads and installs if not present
- Prompts for Azure login if not already authenticated
- Sets the correct subscription context
- Resource Group: Creates if it doesn't exist
- App Service Plan: Creates with specified configuration
- Web App: Creates with Python runtime
- Configuration: Sets up Python version and startup command
- Confirms all resources were created successfully
- Displays the web app URL
- Shows configuration summary
After the script completes successfully, you should see:
- Resource Group in Azure Portal
- App Service Plan with Basic tier
- App Service with Python 3.11 runtime
- Web App URL (will show default page until deployment)
If you prefer to create resources manually using Azure CLI:
# Login to Azure
az login
# Set subscription
az account set --subscription "your-subscription-id"
# Create resource group
az group create --name "your-resource-group" --location "swedencentral"
# Create App Service Plan
az appservice plan create --name "your-app-service-plan" --resource-group "your-resource-group" --sku B1 --is-linux
# Create Web App
az webapp create --name "your-app-name" --resource-group "your-resource-group" --plan "your-app-service-plan" --runtime "PYTHON|3.11"
# Configure startup command
az webapp config set --name "your-app-name" --resource-group "your-resource-group" --startup-file "gunicorn --bind=0.0.0.0 --timeout 600 app:app"After creating the Azure Web App, you need to deploy your application code. The project includes automated deployment scripts.
- Ensure the Azure Web App is created (see previous section)
- Verify your code is ready in the
src/directory - Ensure you're authenticated to Azure
-
Double-click
DeployAzureWebApp.bator run from command prompt:DeployAzureWebApp.bat
-
Follow the prompts:
- The script will pause for confirmation before proceeding
- Press any key to continue when prompted
-
What it does:
- Checks if PowerShell 7 is installed
- Unblocks all *.ps1 files
- Runs DeployAzureWebApp.ps1 with PowerShell 7
The PowerShell deployment script performs the following actions:
- Reads and validates the
.envfile - Verifies Azure Web App exists
- Checks authentication status
- Creates a temporary deployment package
- Copies all files from
src/directory - Includes
requirements.txtandapp.py - Creates a ZIP archive for deployment
- Uploads the ZIP package to Azure Web App
- Triggers the deployment process
- Monitors deployment status
- Installs Python dependencies automatically
- Restarts the web app
- Verifies deployment success
- Displays the live web app URL
After successful deployment:
- Visit your web app URL:
https://your-app-name.azurewebsites.net - Test the endpoints:
/- Documentation page/test- Returns timestamp and random number/wikipedia- Fetches Wikipedia content/time- Gets current time
If you prefer to deploy manually using Azure CLI:
# Navigate to project directory
cd /d "e:\dev\Minimal-Azure-WebApp-Python"
# Create deployment package
powershell -Command "Compress-Archive -Path './src/*' -DestinationPath './deployment.zip' -Force"
# Deploy to Azure Web App
az webapp deployment source config-zip --resource-group "your-resource-group" --name "your-app-name" --src "./deployment.zip"
# Clean up
del deployment.zipIf deployment fails:
-
Check logs:
az webapp log tail --name "your-app-name" --resource-group "your-resource-group"
-
Verify requirements.txt:
- Ensure all dependencies are listed
- Check for version compatibility
-
Check startup command:
az webapp config show --name "your-app-name" --resource-group "your-resource-group" --query "appCommandLine"
