Import (Bind) DNS zones into Azure DNS

You can use 2 methods for importing DNS zones into your Azure DNS zone.

  1. Via Azure Cloud Shell using a Bind file (preferred)
  2. Via Powershell and a CSV

Option 1 :

First you need to export you DNS zone from your registrar and edit the file so the SOA record looks like this :

; Exported zone file from Azure DNS
;      Zone name: <Zone Name>
;      Resource Group Name: <RG name> 
;      Date and time (UTC): Tue, 28 Sep 2021 12:11:29 +0000

$TTL 300
$ORIGIN <dns zone name>.
    
@ 3600 IN SOA ns1-06.azure-dns.com. azuredns-hostmaster.microsoft.com. (
              1 ; serial
              3600 ; refresh
              300 ; retry
              2419200 ; expire
              300 ; minimum
              )

  172800 IN NS ns1-06.azure-dns.com.
  172800 IN NS ns2-06.azure-dns.net.
  172800 IN NS ns3-06.azure-dns.org.
  172800 IN NS ns4-06.azure-dns.info.


@	IN	TXT …………………………….

If the file is correctly prepared it’s very easy to import the zone file directly into Azure.
To import the zone file into Azure DNS we are going to use Azure Cloud Shell. 

Start to open you Azure Portal (https://portal/azure.com) and click in the top right corner on the Cloud Shell icon

Now choose to upload your prepared DNS zone file by clicking the upload button

Upload 
Download 
Manage file share Z

After the file is successfully uploaded enter this command :

az network dns zone import --resource-group <ResourceGroupName> --name <Zone Name> --file-name <file Name>

It’s also possible to export the DNS zone file :

az network dns zone export -g Default-Networking  -n test.com  -f azure-dns-exp.txt

When the import is completed successfully the output in your window looks like this :

PS /home/….> az network dns zone import --resource-group … --name ….. --file-name azure-dns-exp.txt
In the future, zone name will be case insensitive.
== BEGINNING ZONE IMPORT: …..l ==

(1/101) Imported 1 records of type 'soa' and name '@'
(5/101) Imported 4 records of type 'NS' and name '@'
(12/101) Imported 7 records of type 'txt' and name '@'
……..
(101/101) Imported 1 records of type…….'

== 101/101 RECORDS IMPORTED SUCCESSFULLY: '…….' ==

Option 2:

  1. Create the CSV file.
    Preference column is the priority
Name 
RecordType 
autodiscover.cloudsh. 
- CNAME 
_sip._tls.wml.nl 
SRV 
ZoneName 
cloudshark.nl 
cloudshark.nl 
cloudshark.nl 
ResourceGroup 
RG-Def-Networking 
RG-Def-Networking 
RG-Def-Networking 
Preference 
3600 
1800 
Weight 
100 
Port 
Value 
wm I. mail.protection.outlook.com 
autodiscover.outlook.com 
443 100 1 443 sipdir.online.lync.com
  1. Use this script to import the records into Azure DNS
$CSVFile = "C:\temp\DNS-import-zones.csv"
$Records = Import-CSV -Path $CSVFile
foreach ($Record in $Records) 
    {
    switch ($Record.RecordType) 
        {
        "A" { New-AzDnsRecordSet -Name $Record.Name -RecordType $Record.RecordType -ZoneName $Record.ZoneName -ResourceGroupName $Record.ResourceGroup -Ttl $Record.TTL -DnsRecords (New-AzDnsRecordConfig -IPv4Address $Record.Value) ;Break } 
        "CNAME" { New-AzDnsRecordSet -Name $Record.Name -RecordType $Record.RecordType -ZoneName $Record.ZoneName -ResourceGroupName  $Record.ResourceGroup -Ttl $Record.TTL -DnsRecords (New-AzDnsRecordConfig -Cname $Record.Value) ;Break } 
        "TXT" { New-AzDnsRecordSet -Name $Record.Name -RecordType $Record.RecordType -ZoneName $Record.ZoneName -ResourceGroupName  $Record.ResourceGroup -Ttl $Record.TTL -DnsRecords (New-AzDnsRecordConfig -Value $Record.Value) ;Break } 
        "MX" { New-AzDnsRecordSet -Name $Record.Name -RecordType $Record.RecordType -ZoneName $Record.ZoneName -ResourceGroupName  $Record.ResourceGroup -Ttl $Record.TTL -DnsRecords (New-AzDnsRecordConfig -Exchange $Record.Value -Preference $Record.Preference) ;Break } 
        "SRV" { New-AzDnsRecordSet -Name $Record.Name -RecordType $Record.RecordType -ZoneName $Record.ZoneName -ResourceGroupName  $Record.ResourceGroup -Ttl $Record.TTL -DnsRecords (New-AzDnsRecordConfig -Priority $Record.Preference  -Weight $Record.Weight -Port $Record.Port   -Target $Record.Value) ;Break }                             
        Default { 
             Write-host "The record " $Record.Name " type is " $Record.RecordType " and can't be Found"
                }
 
        }
         
    }

More info :

Import and export a DNS zone file using the Azure CLI

https://docs.microsoft.com/en-us/azure/dns/dns-import-export

Cloud shell connector in Windows terminal :

The Azure Cloud Shell Connector in Windows Terminal | Windows Command Line (microsoft.com)

Persist files in Azure Cloud Shell

https://docs.microsoft.com/en-us/azure/cloud-shell/persisting-shell-storage

Leave a Reply

Your email address will not be published. Required fields are marked *