#Backup #AWS #Route53 DNS Zones in C#
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.
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);
LikeLike