Azure Traffic Manager Management
The Azure Traffic Manager Management Client Library for Python allows developers to programmatically manage Azure Traffic Manager profiles, endpoints, and configurations. It is part of the Azure SDK for Python, currently at version 1.1.0, and typically follows a frequent release cadence, often coinciding with Azure service updates.
Warnings
- breaking Older Azure Management SDKs (often referred to as Track 1) used `msrestazure.azure_active_directory.AADTokenCredentials` for authentication. Modern Azure SDKs (Track 2, including this library) exclusively use `azure-identity` library for authentication, such as `DefaultAzureCredential`.
- gotcha Traffic Manager profile names (`dns_config.relative_name`) must be globally unique across all of Azure, not just within your subscription or resource group. If the name is already taken, creation will fail.
- gotcha When creating or updating a Traffic Manager profile, the `location` property must always be set to 'global'. Traffic Manager is a global service and does not reside in a specific Azure region.
- deprecated Older methods for working with management clients might be replaced by new patterns (e.g., direct property access vs. `get_` methods). Always refer to the latest SDK documentation and samples for the current idiomatic usage.
Install
-
pip install azure-mgmt-trafficmanager azure-identity
Imports
- TrafficManagerManagementClient
from azure.mgmt.trafficmanager import TrafficManagerManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.trafficmanager import TrafficManagerManagementClient
# --- Environment Variables (for local development) ---
# AZURE_SUBSCRIPTION_ID: Your Azure Subscription ID
# AZURE_CLIENT_ID: Your Azure AD Application (client) ID
# AZURE_CLIENT_SECRET: Your Azure AD Application client secret
# AZURE_TENANT_ID: Your Azure AD Tenant ID
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group_name = "my-traffic-manager-rg"
profile_name = "myuniquetmprofile123" # Must be globally unique
# Authenticate using DefaultAzureCredential
# This credential type tries various auth methods suitable for local dev and production
credential = DefaultAzureCredential()
# Create a TrafficManagerManagementClient
traffic_manager_client = TrafficManagerManagementClient(credential, subscription_id)
print(f"Listing Traffic Manager profiles in subscription {subscription_id}:")
for profile in traffic_manager_client.profiles.list_by_subscription():
print(f"- {profile.name} (Status: {profile.profile_status})")
# Example: Create a simple Traffic Manager profile
# Note: This is a basic example, a real profile would need endpoints
location = "global" # Traffic Manager is a global service
profile_parameters = {
"location": location,
"profile_status": "Enabled",
"traffic_routing_method": "Performance",
"dns_config": {
"relative_name": profile_name, # This forms the globally unique FQDN: <relative_name>.trafficmanager.net
"ttl": 300
},
"monitor_config": {
"protocol": "HTTP",
"port": 80,
"path": "/"
}
}
print(f"\nAttempting to create/update Traffic Manager profile '{profile_name}'...")
poller = traffic_manager_client.profiles.begin_create_or_update(
resource_group_name,
profile_name,
profile_parameters
)
profile_result = poller.result()
print(f"Profile '{profile_result.name}' created/updated successfully.")
print(f"FQDN: {profile_result.dns_config.fqdn}")