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 name.domain.com, 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. https://developer.dnsimple.com/v2/ 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.

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

The above code connects to Azure and creates an A record for test.funkysi1701.com that points at the ip address 1.2.3.4.

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.

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.

This post has already been read 1451 times!

Simon Foster on GithubSimon Foster on LinkedinSimon Foster on Twitter
Simon Foster
Web Developer

I have worked in SysAdmin and IT Management but now work as a Web Developer. I love everything IT related and I am trying to learn as much as I can especially about DevOps. Why not follow me on twitter?