Azure Compute Management Client Library for Python

37.2.0 · active · verified Mon Apr 06

The `azure-mgmt-compute` library is the Microsoft Azure Compute Management Client Library for Python, providing essential functionality for managing Azure compute resources like Virtual Machines, Virtual Machine Scale Sets, Disks, and Galleries. It enables developers to create, configure, manage, and scale Windows and Linux virtual machines programmatically. Currently at version 37.2.0, the library is actively maintained with frequent updates, often on a monthly or bi-monthly release cadence, reflecting ongoing development in the Azure platform.

Warnings

Install

Imports

Quickstart

This quickstart initializes the `ComputeManagementClient` using `DefaultAzureCredential` for authentication, which is the recommended approach for Azure SDKs. It then proceeds to list all virtual machines accessible within the specified Azure subscription. Ensure `AZURE_SUBSCRIPTION_ID` and other necessary Azure Identity environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) are set for successful authentication.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

# Set environment variables for authentication and subscription ID
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
# AZURE_SUBSCRIPTION_ID

subscription_id = os.getenv("AZURE_SUBSCRIPTION_ID", "")
if not subscription_id:
    print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
    exit(1)

# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)
credential = DefaultAzureCredential()

# Create a Compute Management Client
compute_client = ComputeManagementClient(credential, subscription_id)

try:
    # List all virtual machines in the subscription
    print("Listing all virtual machines:")
    for vm in compute_client.virtual_machines.list_all():
        print(f"- VM Name: {vm.name}, Location: {vm.location}")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have set AZURE_SUBSCRIPTION_ID and have appropriate permissions.")

view raw JSON →