Amazon Route53 C# Examples - Reading Records

In a previous article I discussed adding records. In this article I'll discuss how to read the value of a record.


You will need to use the ListResourceRecordSetsRequest class to build a request. This class retrieves a group of records from a specific HostedZone.

HostedZoneId Required Sets the HostedZone you are querying
StartRecordName Optional The first name in the alphabetic ordering of domain names that you want to retrieve. This option in combination with other options can be used to retrieve a single record or a slice of records.
StartRecordType Optional This is the type of the record you would like to retrieve. Leaving this option blank will retrieve any type. Valid values: A | AAAA | CNAME | MX | NS | PTR | SOA | SPF | SRV | TXT
MaxItems Optional The number of items to retrieve


The ResourceRecordSet object is what we could think of a DNS record and is the result of our query. A single ResourceRecordSet object encapsulates the various types of DNS records, their name, value, TTL, and a few other Route53 configurable options. The resulting object will retrieve a list of ResourceRecordSet objects that will contain the Name, TTL, Type, and ResourceRecords. ResourceRecords encapsulate the value of the record.

Example - Query a Single Record

The first example retrieves the value for a specific record, regardless of type.

/// <summary>Reads a value from Route53 matching the name provided</summary>
/// <returns>Returns the string value matching the name, otherwise returns null</returns>
public string ReadValue(string name)  
    var client = AWSClientFactory.CreateAmazonRoute53Client("AccessKeyID", "SecretAccessKeyID")

    var request = new ListResourceRecordSetsRequest()

    var result = client.ListResourceRecordSets(request);
    foreach (var recordSet in result.ListResourceRecordSetsResult.ResourceRecordSets)
        foreach(var resourceRecord in recordSet.ResourceRecords)
            return resourceRecord.Value;
    return null;

This example iterates over the the resulting ResourceRecordSets and retrieves the first value that is found. If no values are found, it will return null. If you wanted to look for a specific type you could add the WithRecordType filter to this method. Usage of this method is shown below:

string name = "";  
var cnameValue = r53client.ReadValue(name);  

This would output "" since the CNAME records for "" has a value of ""

Example - Query a List of Records

This example queries for a list of records of a specific type. It will return the results as a dictionary.

public Dictionary<string, string> ReadRecords(string type)  
    var result = new Dictionary<string, string>();
    var client = AWSClientFactory.CreateAmazonRoute53Client(Authorization.AccessKeyID, Authorization.SecretAccessKeyID);

    var request = new ListResourceRecordSetsRequest()

    var list = client.ListResourceRecordSets(request);
    foreach (var recordSet in list.ListResourceRecordSetsResult.ResourceRecordSets)
        var values = recordSet.ResourceRecords.Select(p => p.Value);
        result.Add(recordSet.Name, string.Join(",", values));                
    return result;

This example coalesces the values for each record into a single comma delimited value that gets added to the dictionary based on the records name. This example could be extended to include name based filtering by using the WithStartRecordName property.

comments powered by Disqus