Azure CDN Management Client Library
The Azure CDN Management Client Library for Python provides programmatic access to manage Azure Content Delivery Network (CDN) resources. It allows users to create, configure, update, and delete CDN profiles, endpoints, and custom domains. Part of the broader Azure SDK for Python, it follows the SDK's consistent design principles and release cadence, typically receiving updates as new Azure API versions are released or significant changes occur in the underlying service, often with minor version bumps and major version updates for breaking changes.
Common errors
-
ModuleNotFoundError: No module named 'azure.mgmt.cdn.cdn_management_client'
cause In older versions of `azure-mgmt-cdn`, the `CdnManagementClient` was sometimes imported from `azure.mgmt.cdn.cdn_management_client`. A breaking change in a newer major version (e.g., 10.0.0 or later) moved the client directly under `azure.mgmt.cdn` to simplify imports.fixUpdate your import statement to `from azure.mgmt.cdn import CdnManagementClient`. -
DefaultAzureCredential failed to retrieve a token from the included credentials
cause This error indicates that `DefaultAzureCredential`, which attempts to authenticate using several methods (environment variables, Azure CLI, managed identity, etc.), could not find valid credentials in any of the configured locations. This often happens due to missing environment variables, an unauthenticated Azure CLI session, or incorrect service principal configuration.fixEnsure you are logged into Azure CLI (`az login`), or set the necessary environment variables (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`). Alternatively, use a more specific credential type if `DefaultAzureCredential` is not suitable for your deployment environment. -
In order to create this CDN profile, please ensure that Microsoft.CDN is listed as a registered Resource Provider in your Azure subscription
cause Before you can deploy CDN resources in an Azure subscription, the `Microsoft.Cdn` resource provider must be registered within that subscription. If it's not registered, Azure doesn't know how to handle CDN-related resource requests.fixRegister the `Microsoft.Cdn` resource provider using the Azure CLI (`az provider register --namespace Microsoft.Cdn`), Azure PowerShell (`Register-AzResourceProvider -ProviderNamespace Microsoft.Cdn`), or through the Azure portal by navigating to Subscriptions -> Resource Providers and registering 'Microsoft.Cdn'. -
The number of profiles created exceeds quota. Please contact support to increase quota. (Code: BadRequest)
cause Azure subscriptions have default limits (quotas) on the number of certain resources that can be created. This error occurs when you attempt to create a new CDN profile or other CDN-related resource that would exceed your subscription's current quota.fixContact Azure Support to request an increase in your subscription's quota for CDN profiles or the specific CDN resource you are trying to create. Provide your subscription ID and the requested quota limit.
Warnings
- breaking Major version 13.0.0 introduced significant breaking changes, primarily standardizing on `azure-identity` for authentication and `azure-core` for common client primitives. If upgrading from versions prior to 13.0.0, expect changes to authentication mechanisms and potentially to resource model classes and method signatures.
- gotcha Operations that create, update, or delete resources (e.g., `profiles.begin_create`) are often long-running operations and return a 'poller' object (e.g., `LROPoller`). You must call `.result()` on the poller to wait for the operation to complete and retrieve the final resource object or status.
- gotcha Listing operations (e.g., `client.profiles.list()`, `client.endpoints.list_by_profile()`) return an iterable object (e.g., `ItemPaged`), not a direct list. This object automatically handles pagination internally. Attempting to index directly or treat it as a static list without iterating might lead to unexpected behavior or incomplete data.
- gotcha `DefaultAzureCredential` attempts multiple authentication methods. While convenient, it can sometimes be unclear which method is being used or why authentication is failing. For production environments or specific scenarios (e.g., CI/CD, Managed Identity), it's often more robust to explicitly use a specific credential type.
- breaking A `ModuleNotFoundError` for `azure.identity` indicates that the `azure-identity` package, a critical dependency for authentication, is not installed. This prevents any usage of `azure-identity` credentials.
Install
-
pip install azure-mgmt-cdn
Imports
- CdnManagementClient
from azure.mgmt.cdn import CdnManagementClient
- DefaultAzureCredential
from azure.common.credentials import ServicePrincipalCredentials
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.cdn import CdnManagementClient
# --- Configuration ---
# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables.
# You can also set AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET for service principal auth.
AZURE_SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
if not AZURE_SUBSCRIPTION_ID:
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
exit(1)
print(f"Using Subscription ID: {AZURE_SUBSCRIPTION_ID}")
# Authenticate with Azure using DefaultAzureCredential
# This credential chain attempts to authenticate via various methods:
# Environment variables, Managed Identity, Visual Studio Code, Azure CLI, Azure PowerShell.
credential = DefaultAzureCredential()
# Create a CDN management client
# All CDN operations are performed through this client object.
client = CdnManagementClient(credential, AZURE_SUBSCRIPTION_ID)
print("\nListing CDN Profiles in the subscription...")
# List all CDN profiles. This returns an iterator (ItemPaged).
# Iterating directly fetches pages as needed.
cdn_profiles = client.profiles.list()
profile_count = 0
for profile in cdn_profiles:
profile_count += 1
print(f" - Profile Name: {profile.name}, Location: {profile.location}, Sku: {profile.sku.name}")
if profile_count == 0:
print(" No CDN profiles found in this subscription.")
else:
print(f"Found {profile_count} CDN profile(s).")
# Example of a long-running operation (creation of a CDN profile)
# This would typically involve a begin_ method and a poller.
# resource_group_name = "myResourceGroup"
# profile_name = "myUniqueCdnProfile"
# profile_parameters = {
# "location": "westus",
# "sku": {"name": "Standard_Microsoft"}
# }
# print(f"\nStarting creation of CDN Profile '{profile_name}'...")
# poller = client.profiles.begin_create(resource_group_name, profile_name, profile_parameters)
# profile = poller.result() # Wait for the operation to complete
# print(f"CDN Profile '{profile.name}' created successfully.")