Microsoft Azure Resource Deployments Management Client Library for Python
The Microsoft Azure Resource Deployments Management Client Library for Python (`azure-mgmt-resource-deployments`) provides functionality to manage Azure Resource Manager deployments. This library is currently in a beta release (1.0.0b1) and is part of the broader Azure SDK for Python, which typically follows a rapid release cadence for individual service libraries.
Warnings
- breaking This library is currently in beta (1.0.0b1). API signatures, types, and behaviors are subject to change without notice in future releases, potentially leading to breaking changes.
- gotcha The primary client for managing resource deployments, `ResourceManagementClient`, is imported from `azure.mgmt.resource`, not directly from `azure-mgmt-resource-deployments`. This package acts as a meta-package providing specific deployment operations within the broader resource management client.
- gotcha Azure SDKs often provide both synchronous and asynchronous clients. The `ResourceManagementClient` (default) is synchronous. For asynchronous operations, you would typically need to import `ResourceManagementClient` from `azure.mgmt.resource.aio` and use an `async` context.
- gotcha Authentication with Azure services requires setting up credentials correctly. Common issues include missing environment variables (`AZURE_SUBSCRIPTION_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) or not being logged in via `az login` for `DefaultAzureCredential` to work.
Install
-
pip install azure-mgmt-resource-deployments azure-identity
Imports
- ResourceManagementClient
from azure.mgmt.resource import ResourceManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
# --- Setup for Quickstart ---
# Ensure these environment variables are set for authentication and resource creation:
# AZURE_SUBSCRIPTION_ID: Your Azure subscription ID
# AZURE_TENANT_ID: Your Azure Active Directory tenant ID
# AZURE_CLIENT_ID: Your Azure AD application client ID
# AZURE_CLIENT_SECRET: Your Azure AD application client secret
# Or log in via Azure CLI: `az login`
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
resource_group_name = os.environ.get("AZURE_RESOURCE_GROUP_NAME", "my-deployment-test-rg")
deployment_name = "mySimpleStorageDeployment"
location = os.environ.get("AZURE_LOCATION", "eastus")
# Acquire a credential object
try:
credential = DefaultAzureCredential()
except Exception as e:
print(f"Failed to acquire Azure credentials. Please ensure you are logged in (e.g., `az login`) or environment variables are set. Error: {e}")
exit(1)
# Obtain the management object (from azure.mgmt.resource, a dependency)
resource_client = ResourceManagementClient(credential, subscription_id)
# Define a simple ARM template to deploy a storage account
template_content = {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": f"storagename{subscription_id[:8].replace('-','').lower()}", # Unique name
"location": location,
"sku": {"name": "Standard_LRS"},
"kind": "StorageV2",
"properties": {"supportsHttpsTrafficOnly": True}
}
]
}
parameters_content = {}
deployment_properties = {
"mode": "Incremental",
"template": template_content,
"parameters": parameters_content
}
print(f"Creating/Updating resource group '{resource_group_name}' if it doesn't exist...")
resource_client.resource_groups.create_or_update(
resource_group_name,
{"location": location}
)
print(f"Resource group '{resource_group_name}' ready.")
print(f"Initiating deployment '{deployment_name}' in resource group '{resource_group_name}'...")
try:
poller = resource_client.deployments.begin_create_or_update(
resource_group_name,
deployment_name,
{"properties": deployment_properties}
)
deployment_result = poller.result()
print(f"Deployment '{deployment_name}' status: {deployment_result.properties.provisioning_state}")
if deployment_result.properties.provisioning_state == "Succeeded":
print(f"Deployment successful. Resources created:")
for resource in deployment_result.properties.output_resources:
print(f"- {resource.id}")
else:
print("Deployment failed or is in a non-succeeded state.")
if deployment_result.properties.error:
print(f"Error details: {deployment_result.properties.error.code} - {deployment_result.properties.error.message}")
except Exception as e:
print(f"An error occurred during deployment: {e}")