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.
Common errors
-
ModuleNotFoundError: No module named 'azure.mgmt.resource.resources'
cause The `Deployments` and `DeploymentOperations` operation groups were moved from `azure-mgmt-resource` to the separate `azure-mgmt-resource-deployments` package. Older code trying to import or access these directly from `azure.mgmt.resource` will fail.fixEnsure `azure-mgmt-resource-deployments` is installed and update your imports. Instead of accessing `deployments` from `ResourceManagementClient`, you need to import `DeploymentsManagementClient` directly. ```python # Old (incorrect) way # from azure.mgmt.resource import ResourceManagementClient # client = ResourceManagementClient(credential, subscription_id) # deployments_client = client.deployments # Correct way from azure.mgmt.resource.deployments import DeploymentsManagementClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() subscription_id = "your-subscription-id" deployments_client = DeploymentsManagementClient(credential, subscription_id) ``` -
DeploymentFailed
cause This is a generic error indicating that an Azure Resource Manager (ARM) deployment initiated by the client library failed. The actual specific error details are nested within the deployment's status message.fixTo diagnose, retrieve the detailed error message from the deployment operations. You can access the `properties.error` of the deployment object or use Azure CLI/Portal to inspect deployment operation details. The fix depends on the specific inner error (e.g., 'ResourceNotFound', 'BadRequest', 'Conflict'). ```python # After attempting a deployment with deployments_client.deployments.begin_create_or_update(...) # and polling for its completion, if the provisioning_state is 'Failed': deployment_result = \# ... result of polling the LRO if deployment_result.properties.provisioning_state == "Failed": print(f"Deployment failed: {deployment_result.properties.error.code}") print(f"Error message: {deployment_result.properties.error.message}") if deployment_result.properties.error.details: for detail in deployment_result.properties.error.details: print(f" Detail: {detail.message}") ``` -
AuthorizationFailed
cause The Azure account or service principal used to authenticate with the client library lacks the necessary permissions (e.g., 'Contributor' or 'Owner' role) to perform the deployment operations in the specified scope.fixEnsure that the identity you are using (e.g., the service principal or user account) has sufficient Azure role-based access control (RBAC) permissions on the target resource group or subscription. Check the role assignments in the Azure portal under Access control (IAM) for your subscription or resource group. You may also need to register the required resource providers. -
ResourceNotFound
cause The deployment template or parameters refer to an Azure resource that does not exist, has been misspelled, or is in a different resource group or subscription than specified.fixVerify that all resource names, resource group names, and subscription IDs in your deployment template and parameters are correct and that the resources exist in the expected locations. Ensure that there are no missing dependencies where one resource tries to reference another that hasn't been created yet.
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_deployments import 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}")