Back to snippets

azure_mgmt_sdk_create_vm_with_vnet_nic_networking.py

python

Authenticates with Azure using DefaultAzureCredential and creates a V

15d ago92 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azure_mgmt_sdk_create_vm_with_vnet_nic_networking.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.compute import ComputeManagementClient
4from azure.mgmt.network import NetworkManagementClient
5from azure.mgmt.resource import ResourceManagementClient
6
7# Acquire a credential object using CLI-based auth or Environment variables
8credential = DefaultAzureCredential()
9
10# Retrieve subscription ID from environment variable
11subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
12
13# Constants for the resource names
14RESOURCE_GROUP_NAME = "PythonAzureQuickstart-rg"
15LOCATION = "eastus"
16VNET_NAME = "python-example-vnet"
17SUBNET_NAME = "python-example-subnet"
18IP_NAME = "python-example-ip"
19NIC_NAME = "python-example-nic"
20VM_NAME = "python-example-vm"
21USERNAME = "azureuser"
22PASSWORD = "Password1234!"
23
24# Initialize clients
25resource_client = ResourceManagementClient(credential, subscription_id)
26network_client = NetworkManagementClient(credential, subscription_id)
27compute_client = ComputeManagementClient(credential, subscription_id)
28
29# 1. Create Resource Group
30resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, {"location": LOCATION})
31
32# 2. Create VNet
33vnet_poller = network_client.virtual_networks.begin_create_or_update(
34    RESOURCE_GROUP_NAME, VNET_NAME,
35    {"location": LOCATION, "address_space": {"address_prefixes": ["10.0.0.0/16"]}}
36)
37vnet_result = vnet_poller.result()
38
39# 3. Create Subnet
40subnet_poller = network_client.subnets.begin_create_or_update(
41    RESOURCE_GROUP_NAME, VNET_NAME, SUBNET_NAME, {"address_prefix": "10.0.1.0/24"}
42)
43subnet_result = subnet_poller.result()
44
45# 4. Create Public IP
46ip_poller = network_client.public_ip_addresses.begin_create_or_update(
47    RESOURCE_GROUP_NAME, IP_NAME,
48    {"location": LOCATION, "public_ip_allocation_method": "Dynamic"}
49)
50ip_result = ip_poller.result()
51
52# 5. Create NIC
53nic_poller = network_client.network_interfaces.begin_create_or_update(
54    RESOURCE_GROUP_NAME, NIC_NAME,
55    {
56        "location": LOCATION,
57        "ip_configurations": [{
58            "name": "python-example-ip-config",
59            "subnet": {"id": subnet_result.id},
60            "public_ip_address": {"id": ip_result.id}
61        }]
62    }
63)
64nic_result = nic_poller.result()
65
66# 6. Create Virtual Machine
67vm_poller = compute_client.virtual_machines.begin_create_or_update(
68    RESOURCE_GROUP_NAME, VM_NAME,
69    {
70        "location": LOCATION,
71        "storage_profile": {
72            "image_reference": {
73                "publisher": "Canonical",
74                "offer": "UbuntuServer",
75                "sku": "18.04-LTS",
76                "version": "latest"
77            }
78        },
79        "hardware_profile": {"vm_size": "Standard_DS1_v2"},
80        "os_profile": {
81            "computer_name": VM_NAME,
82            "admin_username": USERNAME,
83            "admin_password": PASSWORD
84        },
85        "network_profile": {
86            "network_interfaces": [{"id": nic_result.id}]
87        }
88    }
89)
90vm_result = vm_poller.result()
91
92print(f"Provisioned virtual machine {vm_result.name}")