Azure Resource Management Client Library
The `azure-mgmt-resource` library is the Microsoft Azure Resource Management Client Library for Python. It provides programmatic access to the Azure Resource Manager (ARM) API, enabling developers to create, update, query, and delete Azure resources and resource groups. Part of the comprehensive Azure SDK for Python, it's currently at version 25.0.0 and maintains an active development and release cadence.
Warnings
- breaking Version 25.0.0 and later packages now exclusively target the latest API-Version available on Azure. If your application relies on a specific, non-latest API-Version, you must pin the package to a previous version to maintain compatibility.
- breaking Many sub-modules and their corresponding clients, such as `subscriptions`, `features`, `links`, `locks`, `policy`, `managedapplications`, `databoundaries`, `changes`, `privatelinks`, `deploymentstacks`, `deploymentscripts`, and `templatespecs` have been separated from `azure-mgmt-resource` into independent packages or submodules.
- gotcha Many Azure operations, especially resource creation or deletion, are asynchronous and return a `Poller` object. Calling `.result()` on the poller is necessary to wait for the operation's completion and retrieve the final result.
- gotcha The `DefaultAzureCredential` used for authentication relies on environment variables (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`) or other configured authentication mechanisms (like Azure CLI login, Managed Identity) for local development and deployment. Incorrect or missing environment variables will lead to authentication failures.
Install
-
pip install azure-mgmt-resource azure-identity
Imports
- ResourceManagementClient
from azure.mgmt.resource import ResourceManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- SubscriptionClient
from azure.mgmt.resource.subscriptions import SubscriptionClient
- DeploymentsMgmtClient
from azure.mgmt.resource.deployments import DeploymentsMgmtClient
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
# Set your Azure Subscription ID as an environment variable: AZURE_SUBSCRIPTION_ID
# For local development, also set: AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
# Acquire a credential object using DefaultAzureCredential.
# This will attempt to authenticate in various environments.
credential = DefaultAzureCredential()
# Create a Resource Management client
resource_client = ResourceManagementClient(credential, subscription_id)
# Example: List resource groups in the subscription
print("Listing resource groups...")
for rg in resource_client.resource_groups.list():
print(f" Resource Group: {rg.name} (Location: {rg.location})")
# Example: Create a new resource group (uncomment to run)
# RESOURCE_GROUP_NAME = "my-new-resource-group"
# RESOURCE_GROUP_LOCATION = "eastus"
# print(f"Creating resource group '{RESOURCE_GROUP_NAME}' in '{RESOURCE_GROUP_LOCATION}'...")
# creation_result = resource_client.resource_groups.begin_create_or_update(
# RESOURCE_GROUP_NAME,
# {"location": RESOURCE_GROUP_LOCATION}
# ).result() # .result() waits for the long-running operation to complete
# print(f"Resource group '{creation_result.name}' created.")