Dynamic DNS for EC2 instances

Share on:

The problem

Booting instances on EC2 is easy. But once it comes to connecting to them, you end up having to copy around transient and unwieldy ec2-....compute.amazonaws.com host names.

So you manually set up a CNAME in your DNS to give the instance a friendly name, but as soon as you stop and restart the instance, the ec2 name has changed so your DNS record needs updating.

Rapidly gets tedious!

cli53 to the rescue

cli53 is a command line tool (written by myself), that grew up around when Route53 first launched and was direly lacking tools, to help migrating bind zone files into Route53.

I figured it should go on making your life easier, so with the 0.4.0 release I released a shiny new feature:

Dynamic EC2 instance CNAMEs

The simplicity of this is all you need to do is name your instances with the host name you’d like them to have. You may even be doing this already.

Assuming you’ve a domain hosted on route53, it’s simple to get running.

Install cli53:

$ pip install cli53

Configure your Amazon credentials (put this in .bashrc/.zshrc):

$ export EC2_REGION=eu-west-1 (optional: us-east-1 is the default) 

And finally run cli53 instances:

$ cli53 instances mydomain.com

This will create/update CNAMES for each of your named EC2 instances under mydomain.com.

Assuming you have an EC2 instance named ‘app01’, you can now simply:

$ ssh app01.mydomain.com


Stick the command in cron and run it every five minutes:

*/5 * * * * cli53 instances mydomain.com

For more info and further examples, see the cli53 docs.

I’ll write a future blog post on how we integrate this with automatic on/off on non-production instances in order to reduce EC2 costs significantly.