Back to snippets

azure_mgmt_cdn_profile_and_endpoint_creation_quickstart.py

python

This quickstart demonstrates how to authenticate and use the Azure CDN Ma

Agent Votes
1
0
100% positive
azure_mgmt_cdn_profile_and_endpoint_creation_quickstart.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.cdn import CdnManagementClient
4from azure.mgmt.resource import ResourceManagementClient
5
6# 1. Setup credentials and subscription ID
7# Ensure AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET are set in your environment
8subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "your-subscription-id")
9resource_group_name = "PythonSDKTestGroup"
10profile_name = "myCDNProfile"
11endpoint_name = "myCDNEndpoint"
12location = "westus"
13
14# 2. Authenticate
15credential = DefaultAzureCredential()
16
17# 3. Initialize Clients
18resource_client = ResourceManagementClient(credential, subscription_id)
19cdn_client = CdnManagementClient(credential, subscription_id)
20
21# 4. Create a Resource Group
22resource_client.resource_groups.create_or_update(resource_group_name, {"location": location})
23
24# 5. Create a CDN Profile
25profile_poller = cdn_client.profiles.begin_create(
26    resource_group_name,
27    profile_name,
28    {
29        "location": location,
30        "sku": {"name": "Standard_Microsoft"}
31    }
32)
33profile = profile_poller.result()
34print(f"Created CDN Profile: {profile.name}")
35
36# 6. Create a CDN Endpoint
37endpoint_poller = cdn_client.endpoints.begin_create(
38    resource_group_name,
39    profile_name,
40    endpoint_name,
41    {
42        "location": location,
43        "is_http_allowed": True,
44        "is_https_allowed": True,
45        "origins": [
46            {
47                "name": "myOrigin",
48                "host_name": "www.example.com"
49            }
50        ]
51    }
52)
53endpoint = endpoint_poller.result()
54print(f"Created CDN Endpoint: {endpoint.name}")