{"id":21327637,"url":"https://github.com/mostafa-wael/azure-python-sdk-guide-for-beginners","last_synced_at":"2025-04-14T23:42:01.343Z","repository":{"id":157459829,"uuid":"633493069","full_name":"Mostafa-wael/Azure-Python-SDK-Guide-for-Beginners","owner":"Mostafa-wael","description":"In this simple blog, we will learn how to provision virtual machines on Azure using the Python SDK, the steps are nearly the same for the other languages, once you grasp the general idea you will be able to other languages easily.","archived":false,"fork":false,"pushed_at":"2023-04-27T20:19:41.000Z","size":11,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T23:41:49.872Z","etag":null,"topics":["azure","cloud","devops","pyhton"],"latest_commit_sha":null,"homepage":"https://mostafawael.medium.com/how-to-provision-azure-virtual-machines-using-python-sdk-a2a3767256d1","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mostafa-wael.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-27T16:06:42.000Z","updated_at":"2024-02-10T23:00:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"89ba533d-2c0f-4cd2-a82b-4f5ae971465f","html_url":"https://github.com/Mostafa-wael/Azure-Python-SDK-Guide-for-Beginners","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mostafa-wael%2FAzure-Python-SDK-Guide-for-Beginners","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mostafa-wael%2FAzure-Python-SDK-Guide-for-Beginners/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mostafa-wael%2FAzure-Python-SDK-Guide-for-Beginners/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mostafa-wael%2FAzure-Python-SDK-Guide-for-Beginners/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mostafa-wael","download_url":"https://codeload.github.com/Mostafa-wael/Azure-Python-SDK-Guide-for-Beginners/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248981257,"owners_count":21193143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure","cloud","devops","pyhton"],"created_at":"2024-11-21T21:18:39.174Z","updated_at":"2025-04-14T23:42:01.336Z","avatar_url":"https://github.com/Mostafa-wael.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure-Python-SDK-Guide-for-Beginners\nIn this simple blog, we will learn how to provision virtual machines on Azure using the Python SDK, the steps are nearly the same for the other languages, once you grasp the general idea you will be able to other languages easily.\n\n![image](https://user-images.githubusercontent.com/56788883/234921673-c3ed0c29-a092-4d21-8af6-3780cf15588d.png)\n\n## Why the Azure Python SDK?\nThere are several reasons why you might want to use the Azure Python SDK:\n1. Automate Azure operations: The Azure Python SDK provides a convenient and programmatic way to automate Azure operations using Python scripts. This means you can write scripts to create and manage Azure resources, monitor and optimize your Azure infrastructure, and perform other administrative tasks.\n2. Seamless integration with Python: Python is a popular programming language with a large community and a wide range of libraries and frameworks. By using the Azure Python SDK, you can leverage the power and flexibility of Python to build and deploy Azure applications and services.\n3. Cross-platform support: The Azure Python SDK supports multiple platforms, including Windows, macOS, and Linux. This means you can write Python scripts that can run on any platform and manage your Azure resources from anywhere.\n4. Rich functionality: The Azure Python SDK provides access to a wide range of Azure services, including compute, storage, networking, security, and more. This means you can build complex and sophisticated applications that take advantage of Azure’s rich functionality and features.\n5. Faster development: By using the Azure Python SDK, you can save time and effort in developing Azure applications and services. The SDK provides a high-level, Pythonic interface that makes it easy to work with Azure resources and services, reducing the amount of boilerplate code you need to write.\n\n\n### Step 1: Add the required imports\n```python\nfrom azure.mgmt.compute import ComputeManagementClient\nfrom azure.mgmt.network import NetworkManagementClient\nfrom azure.mgmt.resource import ResourceManagementClient\nfrom azure.identity import DefaultAzureCredential\nfrom azure.mgmt.compute.models import HardwareProfile, OSProfile\n```\n\n### Step 2: Create a resource group\nThis function creates a new resource group with the given name and location using the ResourceManagementClient.\n\n```python\ndef create_resource_group(resource_group_name, location, resource_client):\n    resource_client.resource_groups.create_or_update(\n        resource_group_name,\n        {'location': location}\n    )\n    print(f'Created resource group: {resource_group_name}')\n```\n\n### Step 3: Create the virtual network\nThis function creates a new virtual network with the given name and location using the NetworkManagementClient. It also creates a subnet with the name “`mysubnet`” and the address prefix “`10.0.0.0/24`” within the virtual network.\n\n```python\ndef create_virtual_network(resource_group_name, location, network_client):\n    vnet_params = {\n        'address_space': {\n            'address_prefixes': ['10.0.0.0/16']\n        },\n        'subnets': [{\n            'name': 'mysubnet',\n            'address_prefix': '10.0.0.0/24'\n        }]\n    }\n    virtual_network_poller = network_client.virtual_networks.begin_create_or_update(\n        resource_group_name,\n        'myvnet',\n        {\n            'location': location,\n            'address_space': vnet_params['address_space'],\n            'subnets': vnet_params['subnets']\n        }\n    )\n    virtual_network = virtual_network_poller.result()\n    print(f'Created virtual network: {virtual_network.name}')\n\n    subnet = network_client.subnets.get(resource_group_name, virtual_network.name, 'mysubnet')\n    return subnet\n```\n\n### Step 4: Create the network interface\nThis function creates a new network interface with the given name and location using the NetworkManagementClient. It uses the subnet created in the previous function.\n\n```python\ndef create_network_interface(resource_group_name, location, subnet, network_client):\n    nic_poller = network_client.network_interfaces.begin_create_or_update(\n        resource_group_name,\n        'mynic',\n        {\n            'location': location,\n            'ip_configurations': [{\n                'name': 'myipconfig',\n                'subnet': {\n                    'id': subnet.id\n                }\n            }]\n        }\n    )\n    nic = nic_poller.result() # Get the actual NIC object\n    print(f'Created network interface: {nic.name}')\n\n    return nic\n```\n\n### Step 5: Create the virtual machine\nThis function creates a new virtual machine with the given name and location using the ComputeManagementClient. It uses the virtual network and network interface created in the previous functions. It also sets the virtual machine’s size, operating system, administrator username, and password.\n\n\n```python\ndef create_virtual_machine(resource_group_name, location, compute_client, network_client, nic):\n    vm_name = 'myvm'\n    vm_size =  'Standard_B1ls'#'Standard_D1_v2'\n    image_reference = {\n        'publisher': 'Canonical',\n        'offer': 'UbuntuServer',\n        'sku': '18.04-LTS',\n        'version': 'latest'\n    }\n\n    # Create the hardware profile for the VM\n    hardware_profile = HardwareProfile(\n        vm_size=vm_size\n    )\n    # Set the admin username and password for the VM\n    admin_username = '\u003cadmin_username\u003e'\n    admin_password = '\u003cstrong_password\u003e'\n    # Create the OS profile for the VM\n    os_profile = OSProfile(\n        computer_name=vm_name,\n        admin_username=admin_username,\n        admin_password=admin_password\n    )\n\n    vm_poller = compute_client.virtual_machines.begin_create_or_update(\n        resource_group_name,\n        vm_name,\n        {\n            'location': location,\n            'hardware_profile': hardware_profile,\n            'os_profile': os_profile,\n            'storage_profile': {\n                'image_reference': image_reference\n            },\n            'network_profile': {\n                'network_interfaces': [{\n                    'id': nic.id\n                }]\n            }\n        }\n    )\n    vm = vm_poller.result() # Get the actual VM object\n    print(f'Created virtual machine: {vm.name}')\n```\n\n### Step 6: Call them all\nSet up the Azure API client, calls the create/delete functions in order, and pass the necessary parameters.\n```python\nif __name__ == \"__main__\":\n    # Set up the Azure API client\n    credentials = DefaultAzureCredential()\n    subscription_id = '\u003csubscription_id\u003e'\n\n    compute_client = ComputeManagementClient(credentials, subscription_id)\n    resource_client = ResourceManagementClient(credentials, subscription_id)\n    network_client = NetworkManagementClient(credentials, subscription_id)\n\n    # Create a new resource group\n    resource_group_name = 'myresourcegroup'\n    location = 'eastus'\n    create_resource_group(resource_group_name, location, resource_client)\n    subnet = create_virtual_network(resource_group_name, location, network_client)\n    nic = create_network_interface(resource_group_name, location, subnet, network_client)\n    vm = create_virtual_machine(resource_group_name, location, compute_client, network_client, nic)\n```\n\nYou can also check that the resources were created by running: `az resource list --output table`\n\n### Step 7: delete the resources\nThis function deletes the virtual machine, network interface, virtual network, and resource group created in the previous functions using their respective management clients.\n\n```python\ndef delete_resources(resource_group_name, compute_client, network_client, resource_client):\n    vm_name = 'myvm'\n    nic_name = 'mynic'\n    vnet_name = 'myvnet'\n\n    # Delete the VM\n    print(f'Deleting VM: {vm_name}')\n    compute_client.virtual_machines.begin_delete(resource_group_name, vm_name).wait()\n\n    # Delete the NIC\n    print(f'Deleting NIC: {nic_name}')\n    network_client.network_interfaces.begin_delete(resource_group_name, nic_name).wait()\n\n    # Delete the virtual network\n    print(f'Deleting virtual network: {vnet_name}')\n    network_client.virtual_networks.begin_delete(resource_group_name, vnet_name).wait()\n\n    # Delete the resource group\n    print(f'Deleting resource group: {resource_group_name}')\n    resource_client.resource_groups.begin_delete(resource_group_name).wait()\n```\n\nDon't forget to start the repo of you found this uesful!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmostafa-wael%2Fazure-python-sdk-guide-for-beginners","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmostafa-wael%2Fazure-python-sdk-guide-for-beginners","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmostafa-wael%2Fazure-python-sdk-guide-for-beginners/lists"}