Boto and Amazon Route53

For a while now, I've been working on making the Route53 module for boto much more simple and easy to use. While Route53 doesn't yet provide anything special that you can't find in other DNS providers, it's still quite useful to be able to programmatically change DNS records from within your instances. For example, you could set up an instance to boot and automatically associate itself as another server on a given domain for the purposes of Round Robin DNS, or for setting up Memcache servers where you need to have your servers contact all of the possible memcache servers to determine if there's a cached version of your data available.



Creating your first hosted Zone

Before you begin, you'll need to make sure you sign up for Route53 from Amazon Web Services. It's free to sign up, and you only pay for what you use, so if you have no hosted zones you'll pay nothing for being signed up. It only costs $1/month per hosted zone, plus a minimal charge for each hit against your domain.


To create your domain, use the route53 command line client included with boto.

% ./bin/route53 
Usage: route53 [command]
 add_record - Add a new record to a zone
 cmd - Prints this help message
 create - Create a hosted zone, returning the nameservers
 delete_zone - Delete a hosted zone by ID
 get - Get all the records for a single zone
 help - Prints this help message
 ls - List all hosted zones


We want to use the create command to make your first domain.

% ./bin/route53 create example.com 
Pending, please add the following Name Servers:
 ns-xxx.awsdns-22.com
 ns-xxx.awsdns-32.net
 ns-xxxx.awsdns-12.co.uk
 ns-xxxx.awsdns-60.org


After you create the zone, the script will tell you where to point your domain. This must be done from wherever you purchased your domain.

As you can see, Amazon's DNS servers are located around the globe so that no matter where your end-user is, they can have a DNS server close by.

Adding a record to a Zone

Of course, without any records, this zone configuration is relatively useless, so lets set up our records. Before we can add a new record, we need to look up the ID for the zone we just added:

% ./bin/route53 ls         
================================================================================
| ID:   ZXXXXXXXXXXXXXX
| Name: example.com.
| Ref:  xxxxxxx-xxx-xxxx-xxxx-xxxxxxxxxxxx
================================================================================
{}

Next we use that to add our A record, using the Command line function:

% ./bin/route53 add_record ZXXXXXXXXXXXXXX example.com. A 127.0.0.1    
{u'ChangeResourceRecordSetsResponse': {u'ChangeInfo': {u'Status': u'PENDING', u'SubmittedAt': u'2011-02-05T01:34:20.912Z', u'Id': u'/change/XXXXXXXXXXXXXXXXX'}}



This change request will take a while to actually occur, so just sit back and wait for the change to happen, or continue on adding records. You can add any type of record, A, AAAA, CNAME, MX, TXT... etc..


Adding records through boto

Of course, the real advantage is probably not through the command line functionality, but through the ability to change your records programmatically. For example, you could create the following script to automatically add the current instance to your zone if it's a standard boto-based image:


import boto
from boto.route53.record import ResourceRecordSets
conn = boto.connect_route53()
changes = ResourceRecordSets(conn, "ZXXXXXXXXXXXXXX")
change = changes.add_change("CREATE", boto.config.get("dns", "name"),"CNAME")
change.add_value(boto.config.get("Instance", "public-hostname"))
changes.commit()

What's next?

Eventually, Amazon plans to add in Geographic support which will direct your end users to different IPs based on where they're located. This means you can have EC2 servers (or load balancers) set up in different Regions, and depending on where your user is sitting, they'll get the server closest to them!
2