C# REST Client for Amazon Route 53

This example implements GET and POST requests for an Amazon Route 53 client in C#.

Introduction

This example implements GET and POST requests for an Amazon Route 53 client in C#. The AWS SDK for .NET does not currently support Route 53, and the Perl script tool, called DNSCurl.pl, doesn't work on Windows without considerable adaptation. This code runs the API methods described in the AWS Route 53 API documentation with XML post data, result data, and error messages.

To use any of the Route 53 API methods, you have to create an authentication signature. The Route 53 signature is based on the date and the user’s access key. The following method encrypts the signature using the HMACSHA1 algorithm. The method gets the formatted date as an input parameter from a method described with those following this method. An instance of the System.Security.Cryptography.HMACSHA1 class does the work as a variable named MySigner. After the signature is created, it is base 64 encoded using a static method of the Convert class.

The code for a complete client project is shown below. Each method is a REST implementation that interacts with an AWS Route 53 API method described in the Amazon Web Services documentation. The most interesting in the group is the method ChangeResourceRecordSet, which requires POST data as described in the API documentation topic. To use this method, or for that matter any of these methods, some research into the Domain Name System (DNS) will be helpful. Amazon’s implementation of ChangeResourceRecordSet uses XML data that is identified by the last parameter of this method, postFile. The data is sent as an HttpWebRequest POST. One thing that can create problems in this procedure is that the request may be initialized automatically to expect a 100 response before it sends the data. This code works around this by inserting this line of code:

request.ServicePoint.Expect100Continue = false;

Beyond that, the POST sets up the headers, including the Route 53 date and authentication signature from the methods described above. The POST data is read into a byte array and then into the request stream by the following code segment:

Comments and Discussions

One issue I ran into while implementing it though was that sometimes, often after multiple requests, the code would hang. After an hour or so of troubleshooting, it appears that the HttpWebResponse's aren't disposed of properly in the GetRoute53Date() method.

After sticking everything in using statements though it all works like a charm.

Cheers

p.s. For anyone implementing this, another tidbit of info: in the code you can see lines like "string url = "https://route53.amazonaws.com/2010-10-01/hostedzone/" "

However on the amazon api guidelines they use 2011-05-05 in their XML. It doesn't matter which date you choose but you need to be sure these match.