Azure Traffic Manager Management

1.1.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with `DefaultAzureCredential`, create a `TrafficManagerManagementClient`, list existing Traffic Manager profiles, and create a new basic profile. Ensure your environment variables for Azure authentication are set, or replace placeholders with actual values.

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}")

view raw JSON →