3
votes

I have multiple AWS EC2 instances running, and I use Route 53 for public DNS.

I know that I can point Route 53 DNS records to the public IP address or the public DNS name of an instance. These two values change, however, whenever an instance is started or stopped, so every time an instance is stopped, I need to reconfigure Route 53.

Is there any way to statically link an AWS Route 53 record to an EC2 instance, either by instance name, private IP address, private DNS name, or some other identifier?

Obviously, for the DNS record to work for the public, Route 53 would have to resolve the new DNS record to either a public IP address or a public DNS name. I'm just hoping that Route 53 will substitute the current public IP address for an EC2 instance for whatever static identifier it might use to statically link a DNS record to the EC2 instance.

2

2 Answers

6
votes

You could run a script on startup....here's one in Python:

import boto.route53
import requests

myMachine = "youralias"
myZone = "yourdomain.com."

myARecord = myMachine + "." + myZone

myPublicIP = requests.get("http://169.254.169.254/latest/meta-data/public-ipv4")
myCurrIP = myPublicIP.text

conn53 = boto.route53.connect_to_region("universal")

upd53 = conn53.get_zone(myZone)

exists = upd53.get_a(myARecord)
if exists:
  upd53.update_a(myARecord, myCurrIP)
  print "DNSLOG: " + myARecord + " updated to " + myCurrIP
else:
  upd53.add_a(myARecord, myCurrIP)
  print "DNSLOG: " + myARecord + " created: " + myCurrIP

Note you'll either need to be using an EC2 role for the instance that has rights to update Route53 records, or you'll have to add AWS credentials to the system or the script. (see http://boto.readthedocs.org/en/latest/boto_config_tut.html for help with that).

There's also a commandline in the AWS CLI that I think would do it...run:

aws route53 change-resource-record-sets help 

to see the format. It takes a JSON blob...you might have to delete then create, there doesn't seem to be an update (I haven't tried that particular approach so can't say for certain).

3
votes

Use Elastic IP addresses and assign them to your EC2 instances. Configure Route 53 to resolve your DNS entries to those IP addresses.