Creating DNS records programmatically

DNS is the backbone of the internet. It converts domain names into IP addresses. But how can we do clever things with it?

Making DNS changes has always been a pain. You have to remember what company you registered your domain name with, then locate the login details for their website, login and fill in some web form and wait for the changes to replicate across the internet.

There are various websites that assign you a custom subdomain name like, obviously they must do this programmatically, they don’t have a guy following the above process.

Lets see how we could do this.

My first thought was to make use of the DNSimple API. The API allows all sort of DNS changes to be made, however this is a paid service so let’s look at other options.

Azure now has a DNS section. You can go to the portal, add a DNS zone, and then add as many A, CNAME and MX records as you need. This is of course no better than doing it manually.

Azure offers a SDK and an example Visual Studio project. Lets look at how it works.

First you need to setup some credentials to use your Azure subscription. The easiest way to do this is with powershell.

$sp = New-AzureRmADServicePrincipal DisplayName exampleapp Password "{provide-password}"
Sleep 20
New-AzureRmRoleAssignment RoleDefinitionName Contributor ServicePrincipalName $sp.ApplicationId

This will create a AD Service Principal which has access to your Azure subscription.

You need to get the following IDs from the Azure portal.

subscriptionId for your subscription
tenantId or the Azure AD Directory ID
applicationId of the service principal created above
password you entered into the powershell script above

var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var dnsClient = new DnsManagementClient(serviceCreds);
dnsClient.SubscriptionId = subscriptionId;

var recordSetParams = new RecordSet();
recordSetParams.TTL = 3600;
recordSetParams.ARecords = new List<ARecord>();
recordSetParams.ARecords.Add(new ARecord(""));
var recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, "", "test", RecordType.A, recordSetParams);

The above code connects to Azure and creates an A record for that points at the ip address

Other DNS records can be created in a similar way.

The above example creates a new RecordSet but you can use the following to delete or get existing records.

var recordSet = dnsClient.RecordSets.Get(resourceGroupName, "", "test", RecordType.A);

dnsClient.RecordSets.Delete(resourceGroupName, "", "test", RecordType.A);

Now I have found out that I can write a console app to edit my DNS records I need to change the nameservers for all my domains so I can take advantage of this new feature.


comments powered by Disqus