Microsoft Azure Resource Deployments Management Client Library for Python

1.0.0b1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to create an Azure Resource Group and then deploy a simple ARM template (creating a Storage Account) using `azure-mgmt-resource-deployments`. It requires `azure-identity` for authentication and relies on `AZURE_SUBSCRIPTION_ID`, `AZURE_RESOURCE_GROUP_NAME`, and `AZURE_LOCATION` environment variables or a prior `az login`.

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}")

view raw JSON →