Azure DNS Management Client Library

9.0.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential`, create an instance of `DnsManagementClient`, and perform basic operations like creating and listing DNS zones. Remember to set your `AZURE_SUBSCRIPTION_ID` environment variable and ensure your credentials are configured (e.g., via Azure CLI login).

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

view raw JSON →