Home > Uncategorized > #Backup #AWS #Route53 DNS Zones in C#

#Backup #AWS #Route53 DNS Zones in C#

route53

If you host your DNS on Amazon Route53, and it’s reasonably complex, it’s worthwhile taking a backup of it, so that future errors can be undone quickly. There are some tools to do it, however, you can also write a script yourself in C# to do it.

First off, get the NUGET package

Install-Package AWSSDK

Create an IAM user within AWS with programmatic access, and get it’s Access Key and Secret ID. – I’ve given it Administrator access, but you should limit the scope to the minimum.

Then I output all the zones to the console here;

var client = AWSClientFactory.CreateAmazonRoute53Client(“***”, “***”, RegionEndpoint.USEast1);

var strOutput = @”route53backup.json”;

var req = new ListHostedZonesRequest();
for (; ; )
{
var zones = client.ListHostedZones(req);
foreach (HostedZone zone in zones.HostedZones)
{

Console.WriteLine(zone.Name);

var recordSets = client.ListResourceRecordSets(new ListResourceRecordSetsRequest() { HostedZoneId = zone.Id });

foreach (var record in recordSets.ResourceRecordSets)
{
Console.WriteLine(“\t” + record.Name + ” (” + record.Type + “)”);

foreach (var resource in record.ResourceRecords)
{
Console.WriteLine(“\t\t” + resource.Value);

}
}
}
req.Marker = zones.NextMarker;
if (req.Marker == null)
{
break;
}
}

Note that the¬†ListHostedZones call only returns 100 domains at a time, you have to set the “marker” parameter to the value of the NextMarker property that was returned on the previous call to get all records.

 

Advertisements
Categories: Uncategorized
  1. January 16, 2018 at 12:38 pm

    And here’s some code to update a record in AWS route 53

    var change = new Change
    {
    Action = “UPSERT”,
    ResourceRecordSet = new ResourceRecordSet
    {
    Name = “xxxx.xxxxx.com”,
    Type = “A”,
    TTL = 300,
    ResourceRecords = new System.Collections.Generic.List
    {
    new ResourceRecord()
    {
    Value = “1.1.1.1”
    }
    }
    }
    };
    var batch = new ChangeBatch
    {
    Changes = new System.Collections.Generic.List()
    };
    batch.Changes.Add(change);
    var request = new ChangeResourceRecordSetsRequest
    {
    HostedZoneId = “/hostedzone/xxxxxxx”,
    ChangeBatch = batch
    };
    client.ChangeResourceRecordSets(request);

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: