Azure DNS with the Cross-Platform CLI

The Azure DNS service is very powerful. It must be anyways, as the only way to interact with it is directly via the API. That said, it’s a world-class service that can easily handle all the traffic you could possibly get, and is quite robust as well. In addition to all of that, hosting your DNS in Azure is also quite cheap.

Tip: If you want to skip mashing your head against the Azure CLI check out my open source Azure DNS management tool that makes managing your DNS quick and painless.

Setting Up the Azure CLI

First off you’re going to need the Azure CLI installed, and up to date. If you have npm this is a simple npm install -g azure-cli from your favourite *nix terminal. Following that you need to login to the Azure CLI using an organizational account, not a Microsoft account.

Create an Azure Organization Account if you don’t already have one. Once you’ve done this you can log into on the Azure CLI using the below command, replacing the email address with your ORGID email.

azure login -u <orgid_email>

Azure Resource Groups, DNS Zones, Record Sets, and Records

Azure organizes DNS records using four concepts: Resource Groups, DNS Zones, Record Sets, and Records.

Azure Resource Groups are purely organization groups of any type of Azure resource – virtual machines, storage, DNS, etc… They are for your organizational purposes. Every resource must be in a resource group, and you can manage your spending or delete entire resource groups easily. To create a resource group use:

azure group create --name <resourceGroupName> --location eastus2

DNS Zones correspond to your domains. You’ll have a DNS zone for every domain, with the name being the name of the domain. To create a DNS Zone use:

azure network dns zone create <resourceGroupName> <domainName>

DNS Record Sets are all of the records of the same type and path for a DNS zone. For example if you had multiple A records for path.example.com they would all be in the same DNS Record Set. But your A records for path.example.com and otherthing.example.com would lie in different record sets. The same is true for the A records and TXT records for path.example.com — they will live in different record sets because their type (A, TXT, CNAME, etc…) and their path (path.example.com) are different. All records in a record set also share a TTL (Time To Live).

Creating a record set is done like so:

azure network dns record-set create <resourceGroupName> <domainName> <path> <type>

So to create an A record for example.michaelblouin.ca in the resource group dnsGroup with a TTL of 3600 seconds you would use the following command:

azure network dns record-set create dnsGroup michaelblouin.ca example A -l 3600

The above command creates the record set, however there is not yet a record in this record set — so a DNS query for example.michaelblouin.ca will not yet return everything. The next step is to create the record in the record set.

Creating DNS Records

Now that you have a resource group, dns zone, and record set, you need to add records to the record set that will be returned by the DNS server. The syntax for this command is below:

azure network dns record-set add-record <resourceGroupName> <domainName> <path> <type> [options]

Not the inclusion of [options] at the end. You will need to use a flag to set the record value, and the flag depends on the record type. To add an A record for example.michaelblouin.ca pointing to 1.1.1.1 use the following command:

azure network dns record-set add-record dnsGroup michaelblouin.ca example A -a 1.1.1.1

To find out the flag you need to use for a particular record type use the help command:

azure network dns record-set add-record --help

A Better Way to Manage Azure DNS

The CLI and REST APIs are great, but for anyone just looking to host their DNS in Azure for a website whose DNS records don’t change very often they are almost completely unwieldy. For this reason I’ve created an Azure DNS Management Tool that allows you to  write all of your DNS records into a simple text file, and apply them to Azure using a single command. This tool saves you a massive amount of time, allows you to store your DNS records in version control, and is completely free and open source. The tool can even import DNS records from existing DNS servers or existing Azure DNS so you don’t have to manually write the whole config for an existing domain. Check it out! I’m already hosting all of my domains on Azure and it hasn’t taken long at all.

,

No comments yet.

Leave a Reply

Proudly made in Canada