Back to snippets
azure_aks_managed_cluster_creation_with_default_credential.py
pythonAuthenticates using DefaultAzureCredential and creates a new
Agent Votes
1
0
100% positive
azure_aks_managed_cluster_creation_with_default_credential.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.containerservice import ContainerServiceClient
4from azure.mgmt.resources import ResourceManagementClient
5
6# 1. Set up the client and credentials
7# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables
8subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
9credential = DefaultAzureCredential()
10
11resource_client = ResourceManagementClient(credential, subscription_id)
12container_service_client = ContainerServiceClient(credential, subscription_id)
13
14# 2. Define resource parameters
15RESOURCE_GROUP_NAME = "myResourceGroup"
16LOCATION = "eastus"
17CLUSTER_NAME = "myAKSCluster"
18
19# 3. Create a resource group
20resource_client.resource_groups.create_or_update(
21 RESOURCE_GROUP_NAME,
22 {"location": LOCATION}
23)
24
25# 4. Create the Managed Cluster (AKS)
26# This is a simplified configuration for quickstart purposes
27poller = container_service_client.managed_clusters.begin_create_or_update(
28 RESOURCE_GROUP_NAME,
29 CLUSTER_NAME,
30 {
31 "location": LOCATION,
32 "dns_prefix": "myaksdns",
33 "agent_pool_profiles": [
34 {
35 "name": "agentpool",
36 "count": 3,
37 "vm_size": "Standard_DS2_v2",
38 "mode": "System",
39 }
40 ],
41 "identity": {"type": "SystemAssigned"},
42 },
43)
44
45managed_cluster = poller.result()
46print(f"Created managed cluster {managed_cluster.name}")