Azure DNS Management Client Library
The `azure-mgmt-dns` library provides a client for managing Azure DNS zones and record sets, including creating, updating, and deleting DNS resources within your Azure subscriptions. It is part of the Azure SDK for Python and is currently at version 9.0.0, with updates typically released as new API versions become available or significant features are added to the service.
Warnings
- breaking Version 9.0.0 introduced significant breaking changes, primarily impacting API models and operation signatures. Specifically, the `DnsManagementClient.dns_resource_reference` operation was removed, and model types for parameters like `dns_resource_reference_request` and `zones_input` changed from `_models.Type` to the direct `Type` reference (e.g., `DnsResourceReferenceRequest`).
- breaking The signatures for long-running operations such as `dns_client.zones.begin_create_or_update` and `dns_client.zones.begin_delete` have changed in version 9.x. The `api_version` parameter, which specifies the target Azure DNS API version, is now often required as a keyword argument rather than a positional argument.
- gotcha Many resource management operations in Azure SDKs are long-running and return an `LROPoller` object (e.g., `begin_create_or_update`). You must call `.result()` on the poller to wait for the operation to complete and retrieve the final resource object. In asynchronous contexts, you would `await poller`.
- gotcha When working with DNS record sets, the `record_type` (e.g., 'A', 'AAAA', 'CNAME') is crucial. The properties for each record type (e.g., `a_records`, `aaaa_records`) are mutually exclusive within a record set definition. Attempting to define properties for multiple record types in one call will result in an error or unexpected behavior.
Install
-
pip install azure-mgmt-dns azure-identity
Imports
- DnsManagementClient
from azure.mgmt.dns import DnsManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.dns import DnsManagementClient
# Set environment variables for authentication and subscription
# AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for Service Principal
# Or configure AZURE_SUBSCRIPTION_ID
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_AZURE_SUBSCRIPTION_ID')
resource_group_name = 'my-dns-rg'
zone_name = 'example.com'
# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)
credential = DefaultAzureCredential()
dns_client = DnsManagementClient(credential, subscription_id)
# Example: Create a DNS Zone
print(f"Creating DNS Zone '{zone_name}' in resource group '{resource_group_name}'...")
zone_parameters = {
'location': 'global' # DNS zones are 'global' for location
}
# Use begin_create_or_update and then .result() for long-running operations
# In version 9.x, api_version is often required as a kwarg for begin_ operations
zone_creation_poller = dns_client.zones.begin_create_or_update(
resource_group_name,
zone_name,
zone_parameters,
api_version='2018-05-01'
)
zone = zone_creation_poller.result()
print(f"Created Zone ID: {zone.id}")
# Example: List DNS Zones in a resource group
print(f"Listing DNS zones in resource group '{resource_group_name}':")
for dns_zone in dns_client.zones.list_by_resource_group(resource_group_name):
print(f"- {dns_zone.name}")
# Example: Delete a DNS Zone (uncomment to run)
# print(f"Deleting DNS Zone '{zone_name}'...")
# dns_client.zones.begin_delete(resource_group_name, zone_name, api_version='2018-05-01').result()
# print("Zone deleted.")