Luke Smith Write today, wrong tomorrow

Updating a pointhq dns record from amazon ec2 instances

If you're using Amazon EC2 for any reason, but not running the instance 24/7 (I have an instance with TeamCity that I only want to run on the evenings/weekends when I'm doing any work) you may not want to spend the extra on an Elastic IP to get a static IP address.

Unless you use Elastic IP however, everytime you start your EC2 instance you get allocated a new dynamic address, annoying if you want to use a friendly hostname. So how can you  get your dns server updated when your instance starts?

My dns host of choice is PointHQ, they give 10 free domains and provide a useful api for managing the zones and records. Theres even a ruby gem.

Step 1 - Create an A record in PointHQ

First of all we need to create the record in Point. For my TeamCity instance i've created a A record for teamcity.example.com and put a temporary IP address as the records data.

Step 2 - Create a ruby script

The next step is to create a script that can be run to get the machines current public IP address and then to call the Point api. For this you will need to get your api key from the Point website.

To get the current IP address of the EC2 instance you need to make a GET request

ip = open('http://169.254.169.254/latest/meta-data/public-ipv4').read

Using the Point ruby gem we now need to get hold of the zone record hosted by Point and update the data with the instances public IP address.

zone = Point::Zone.find(<ZoneId>) # example.com
record = zone.record(<RecordId>) # teamcity.example.com
record.data = ip
record.save

Run the ruby script now and check PointHQ. The data field of the A record created in step 1 should now be updated with the public IP address of the EC2 instance.

Step 3 - AutoRun the script

We've got the script working updating Point, but it's pretty useless if we still need to remote into the server and run it manually. As I'm running on Windows I've used the Task Scheduler to automatically run the ruby script. I couldn't find a way to be notified when the EC2 instance starts so used the following triggers which update Point fairly quickly after starting the instance.

The only problem I've had is dns caching, which you can flush locally easily. I use opendns which gets updated incredibly quickly and if not theres a handy cachecheck + refresh tool.

Now with hosted YouTrack from JetBrains, and GitHub, I have a fixed url that I can point to for integrating with TeamCity.
comments powered by Disqus