Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Restore AWS Route53 DNS records to Azure DNS

Posted on August 17, 2019February 27, 2022

This is the Part 2 of the two part series of Backup of AWS Route53 DNS records and restoring in Azure DNS using an Azure Automation Runbook.

Please refer Part 1 for Backup of DNS records to Azure Blob storage.

You should have following prerequisites set up before you run the Runbook:

  1. Azure Blob storage containing DNS Backup in JSON format(Refer Part 1)
  2. An Azure Automation account with permission to the Key Vault , Storage Account and Azure DNS

Keep this Runbook handy when you need to restore DNS records or migrate DNS Hosted zones from AWS to Azure.

Param(
	[Parameter(Mandatory=$true)]
	[String]$saResourceGroup,
	[Parameter(Mandatory=$true)]
	[String]$storageAccount,
	[Parameter(Mandatory=$true)]
        [String]$storageContainer,
	[Parameter(Mandatory=$true)]
	[String]$dnsResourceGroup		
)

#declare some variables to be used in the script
$dirName = $fName = $destPath = ""
#endregion

#region - login to Azure 
$connectionName = "AzureRunAsConnection"
try{
    #Get the connection 'AzureRunAsConnection'
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch{
    if(!$ServicePrincipalConnection){
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    }else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
#endregion

#region - Find the lastest backup file to restore from
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $saResourceGroup -Name $storageAccount).Context
$fileName = (Get-AzureStorageBlob -Container $storageContainer -Context $saContext  | Sort-Object LastModified -Descending | Select-Object Name -First 1).Name
Write-Output "File Name : $($fileName)"
if([string]::IsNullOrEmpty($fileName)){
    Write-Output "ERROR: No file found.Restoration will Terminate now."
    Exit
}
if($fileName.Contains("/")){
    $dirName = $fileName.Split("/")[0]
    $fName = $fileName.Split("/")[1]
}
#download blob contents to local file

$download = Get-AzureStorageBlobContent -Blob $fileName -Container $storageContainer -Context $saContext -Destination $env:TEMP -Force -CheckMd5
if([string]::IsNullOrEmpty($download)){
    Write-Output "ERROR: Blob content was not downloaded successfully. Please check the issue.Script will Exit now!"
    Exit
}
if(!([string]::IsNullOrEmpty($dirName))){
    $destPath = "$($env:TEMP)\$($dirName)\$($fName)"
}
else{
    $destPath = "$($env:TEMP)\$($fName)"
}
if(!(Test-Path $destPath)){
    Write-Output "ERROR: File was not downloaded to $($destPath) successfully.Script will Exit now!"
    Exit    
}

$fileData = Get-Content $destPath | ConvertFrom-Json
[PSCustomObject]$fileData | Get-Member -MemberType NoteProperty | ForEach-Object {
    $dnsKey = $_.Name
    [PSCustomObject]@{Key = $dnsKey; Value = $fileData."$dnsKey"} | ForEach-Object {
        $dnsValue = $_.Value
        $dnsHostedZone = $dnsKey.TrimEnd(".")
        Write-Output "Creating Azure DNS Zone Name  - $($dnsHostedZone) in Resource Group $($dnsResourceGroup)"
        New-AzureRmDnsZone -Name $dnsHostedZone -ResourceGroupName $dnsResourceGroup
        foreach($dnsRecord in $dnsValue){         
            $recordName = $dnsRecord.RecordSetName
            $recordName = $recordName.TrimEnd(".")          
            $recordType = $dnsRecord.Type
            $resourceRecord = $dnsRecord.ResourceRecord            
            $recordTTL = $dnsRecord.TTL
            if($recordType -notin "NS","SOA"){
                Write-Output "Adding A DNS Record $($recordName) of Record Type $($recordType) and Resource Records $($resourceRecord) with TTL value as $($recordTTL)"
                switch ($recordType.ToUpper()){
                    "A" {
                        if($resourceRecord){
                            $aRecords = @()
                            foreach($a in  $resourceRecord.Split(";")){
                                $aRecords += New-AzureRmDnsRecordConfig -IPv4Address $a                           
                            }
                            New-AzureRmDnsRecordSet -Name $recordName -RecordType A -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $aRecords 
                        }                        
                    }
                    "PTR" {
                        if($resourceRecord){
                            $ptrRecords = @()
                            foreach($ptr in $resourceRecord.Split(";")){
                                $ptrRecords += New-AzureRmDnsRecordConfig -Ptrdname $ptr
                            }
                            New-AzureRmDnsRecordSet -Name $recordName -RecordType PTR -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $ptrRecords
                        }
                    }                        
                    "CNAME" {
                        if($resourceRecord){
                            $cnameRecords = @()
                            foreach($cname in $resourceRecord.Split(";")){
                                $cnameRecords += New-AzureRmDnsRecordConfig -Cname $cname
                            }
                            New-AzureRmDnsRecordSet -Name $recordName -RecordType CNAME -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $cnameRecords
                        }
                    }                        
                    "TXT" {
                        if($resourceRecord){
                            $txtRecords = @()
                            foreach($txt in $resourceRecord.Split(";")){
                                $txtVal = $txt.replace("\","").replace('""','"')
                                $txtRecords += New-AzureRmDnsRecordConfig -Value $txtVal
                            }
                            New-AzureRmDnsRecordSet -Name $recordName -RecordType TXT -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $txtRecords 
                        }
                    }
                        
                    "MX" {
                        if($resourceRecord){
                            $mxRecords = @()
                            foreach($mx in $resourceRecord.Split(";")){
                                $mxRecords += New-AzureRmDnsRecordConfig -Preference $mx.Split(" ")[0] -Exchange $mx.Split(" ")[1]
                            }
                            New-AzureRmDnsRecordSet -Name $recordName -RecordType MX -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $mxRecords 
                        }                        
                    }
                    "SRV" {
                        if($resourceRecord){
                            $srvRecords = @()
                            foreach($srv in $resourceRecord.Split(";")){
                                $srvRecords += New-AzureRmDnsRecordConfig -Priority $srv.Split(" ")[0] -Weight $srv.Split(" ")[1] -Port $srv.Split(" ")[2] -Target $srv.Split(" ")[3]
                            }
                        }
                        New-AzureRmDnsRecordSet -Name $recordName -RecordType SRV -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $srvRecords
                    }
                }
            }        
        }        
    }
}
#endregion

#region -clean Up File from Temp location
If(Test-Path $destPath){
    Remove-Item $destPath -Force
}
#endregion

Login to Azure Portal using https://portal.azure.com and open Azure Automation Accounts from left pane.

This image has an empty alt attribute; its file name is image-14.png
Login to Azure portal

Open configured Azure Automation account and select Runbooks from Process Automation option in the left pane.

This image has an empty alt attribute; its file name is image-15.png
Create new Runbook

Create a new Runbook using the script above and save it. Once, it is saved run the Runbook from the list.

This image has an empty alt attribute; its file name is image-21.png
Execute Runbook

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
July 2025
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes