Wednesday, December 29, 2010

heading down route 53, dynamic dns for short $

Been hacking about with route53 for a day or so to replace a dynamic dns service. So far so good. The HTTP/XML stuff is a bit tricky, dnscurl.pl is a great reference example, but needed something to be a bit more easy to script around. I was about to bust out some extreme perl XML::Twig parsing action, but the folks over @ boto already have the core part of this nailed! Plus they have some lovely extra stuff in there also for cli based cloud mngt:


http://boto.cloudhackers.com/

Remember to install the python setup tools if you don't have them already 10.04 didn't and I missed the error in the boto build caused me a small amount of churn by not paying attention:
/usr/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: 'install_requires'

Here's the missing piece:
root@shuttle:/home/jcuff# apt-get install python-setuptools

Now the install is cake:
root@shuttle:/home/jcuff# git clone git://github.com/boto/boto
root@shuttle:/home/jcuff# python setup.py build
root@shuttle:/home/jcuff# sudo python setup.py install

Barnaby Gray has a neato little script here that you can use in conjunction with boto and python dns tools:

https://github.com/barnybug/cli53

root@shuttle:/home/jcuff# git clone https://github.com/barnybug/cli53.git

As I mentioned, it has a dep on dnspython (oh and easy_install is awesome for python mod installs!):
root@shuttle:/home/jcuff# easy_install dnspython

Now we are all set with cli53.py I've used an example dns and config below to show function only:
root@shuttle:/home/jcuff# ./cli53.py list
HostedZones:
- CallerReference: jamesdotcuff.1
Config:
Comment: Migrate an existing domain to Route 53
Id: /hostedzone/Z1PZZZZDBXO12345
Name: jamesdotcuff.com.

Even has a very swish bind interface for import and export to the old classic DNS services we are used to, here is an example with an aws A record:
root@shuttle:/home/jcuff# ./cli53.py export jamesdotcuff.com
$ORIGIN jamesdotcuff.com.
@ 172800 IN NS ns-52.awsdns-06.com.
@ 172800 IN NS ns-1479.awsdns-56.org.
@ 172800 IN NS ns-713.awsdns-25.net.
@ 172800 IN NS ns-1891.awsdns-44.co.uk.
@ 900 IN SOA ns-52.awsdns-06.com. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400
aws 600 IN A 192.168.1.1

Interesting how big the AWS ninjas are planning their dns install to be, did you see those host names... ns-xxxx huge eh? :-).

Anyway, now to wrap this in a simple ifconfig script for those on dhcp cable modems and tada - "free" full bore dynamic dns(*):

first delete the old record:
root@shuttle:/home/jcuff# ./cli53.py rrdelete jamesdotcuff.com aws A

and update your dns with the new one for your new IP address that your dsl/cable provider has given you. The script is also smart and allows you a TTL with the -x option. Neat!
root@shuttle:/home/jcuff# ./cli53.py rrcreate -x 600 jamesdotcuff.com aws A 192.168.1.1

and tada! we are basically good to go. I'm sure that AWS will have an "update record" api soon so the whole delete / create thing for rr sets will not be an issue, it's not a big deal breaker - just two calls rather than one. Oh hang on there cowboy! What's this? Looks like Barnaby has thought of everything, there is a --replace option, and a --wait to hang on until the sync is complete. Done, and done:
root@shuttle:/home/jcuff# ./cli53.py rrcreate -x 600 jamesdotcuff.com aws A 192.168.1.2 --replace --wait
Waiting for change to sync.............................completed
ChangeInfo:
Status: INSYNC
SubmittedAt: 2010-12-29T19:01:52.980Z

root@shuttle:/home/jcuff# host aws.jamesdotcuff.com ns-1479.awsdns-56.org
aws.jamesdotcuff.com has address 192.168.1.2

Now that to me is an epic one liner!

Albeit folks always find something to complain about, this stuff is just what I needed. Also a lovely example of wicked fast development in the community. So my personal thanks to Barnaby and all the boto folks, no need for me to give birth to yet another dodgy perl script ;-)

Keep at it AWS ninjas, where ever you all may be!

(*) well 1 buck a month per zone is not so bad ;-)


[any opinions here are all mine, and have absolutely nothing to do with my employer]
(c) 2011 James Cuff