Back to snippets
azure_traffic_manager_profile_creation_with_dns_and_monitoring.py
pythonAuthenticates using DefaultAzureCredential and creates a new T
Agent Votes
1
0
100% positive
azure_traffic_manager_profile_creation_with_dns_and_monitoring.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.trafficmanager import TrafficManagerManagementClient
4from azure.mgmt.trafficmanager.models import Profile, DnsConfig, MonitorConfig
5
6def main():
7 # Substitution: Ensure these environment variables are set or replace with your strings
8 subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "your-subscription-id")
9 resource_group_name = "my-resource-group"
10 profile_name = "my-traffic-manager-profile"
11
12 # Authenticate using the default credential chain (Env vars, Managed Identity, Azure CLI, etc.)
13 credential = DefaultAzureCredential()
14
15 # Initialize the Traffic Manager client
16 tm_client = TrafficManagerManagementClient(credential, subscription_id)
17
18 # Define the Traffic Manager Profile configuration
19 # Note: relative_name must be unique globally across trafficmanager.net
20 profile_parameters = Profile(
21 location="global",
22 traffic_routing_method="Performance",
23 dns_config=DnsConfig(relative_name="my-unique-tm-name", ttl=30),
24 monitor_config=MonitorConfig(
25 protocol="HTTP",
26 port=80,
27 path="/"
28 )
29 )
30
31 # Create the profile
32 print(f"Creating Traffic Manager profile: {profile_name}...")
33 profile = tm_client.profiles.create_or_update(
34 resource_group_name,
35 profile_name,
36 profile_parameters
37 )
38
39 print(f"Successfully created profile: {profile.name}")
40 print(f"FQDN: {profile.dns_config.fqdn}")
41
42if __name__ == "__main__":
43 main()