Azure Web Management Client Library
The `azure-mgmt-web` library provides a Pythonic interface for managing Azure App Services, Azure Functions, and other web-related resources within your Azure subscription. It is part of the Azure SDK for Python, allowing programmatic control over deployments, scaling, configurations, and monitoring of web applications. The current version is 10.1.0, and new versions are released regularly, often aligning with updates to the Azure REST API and broader Azure SDK guidelines.
Warnings
- breaking Major breaking changes occurred in version 9.0.0 regarding client construction and authentication. Older patterns using separate `azure.common.credentials` or `ServicePrincipalCredentials` objects are no longer supported.
- gotcha `DefaultAzureCredential` requires proper environment setup (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) or an active Azure CLI login (`az login`). Without this, authentication will fail.
- gotcha Many list operations (e.g., `list()`, `list_by_resource_group()`) return paginated iterators, not complete lists. You must iterate over the result to access all items.
- gotcha Many Azure Web Management operations are scoped to a specific resource group or location. Omitting these or providing incorrect values will lead to errors or resource not found.
Install
-
pip install azure-mgmt-web azure-identity
Imports
- WebSiteManagementClient
from azure.mgmt.web import WebSiteManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.web import WebSiteManagementClient
# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables,
# or replace 'YOUR_SUBSCRIPTION_ID' directly. Make sure you are also
# logged in via Azure CLI ('az login') or have other AZURE_IDENTITY
# environment variables configured for DefaultAzureCredential to work.
# Retrieve Azure Subscription ID from environment variable
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
print("WARNING: Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID'.")
# Exit or raise error if no subscription ID is available for a real run
# exit(1)
try:
# Authenticate with Azure. DefaultAzureCredential attempts several common auth methods.
credential = DefaultAzureCredential()
# Create a WebSiteManagementClient instance
web_client = WebSiteManagementClient(credential, subscription_id)
print(f"Listing App Service Plans in subscription: {subscription_id}")
# List all App Service Plans in the subscription
app_service_plans = web_client.app_service_plans.list()
found_plans = False
for plan in app_service_plans:
print(f"- Name: {plan.name}, Resource Group: {plan.resource_group_name}, Location: {plan.location}")
found_plans = True
if not found_plans:
print("No App Service Plans found or you may not have access to any.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you are authenticated (e.g., 'az login') and have the correct AZURE_SUBSCRIPTION_ID.")