Adding EC2 instances to Route53

Today’s nifty bit of code is a startup/shutdown script for linux that allows you to add an EC2 instance to Amazon’s Route53 DNS automatically when you start up the instance, and remove it when the instance is knocked down. This script also allows you to add the instance to a weighted round-robin group.

This makes use of the very useful Python-based boto tool which is available in both Yum and Debian repositories under the package name python-boto.

Create this script in /etc/init.d and make it executable, and then add it to the requisite rcX.d directory for startup/shutdown.

Many thanks to Dave McCormick for his blog post on the subject from which I borrowed heavily.

10 Comments On “Adding EC2 instances to Route53”

  1. If I understand this correctly, this script will add the server to Route 53 when it starts up, and remove on a shutdown, but how do you handle the case where a server crashes (or otherwise does not shutdown gracefully), you would really still want to remove it from the weighted round robin, otherwise requests will still resolve to the offline server.

    Reply

    • correct – you could do this with an external monitor that checks for ping and service availability once a server is up,  and removes the server if it goes down. 

      Reply

  2. Pingback: Adding EC2 instances to Route53 | Pinehead.tv

  3. Pingback: Add EC2 to Route53 « 0ddn1x: tricks with *nix

  4. At line 77 you try to create a RR of CNAMEs. Is that right? You can’t do that. You can only create RR of MX, NS and A records, not CNAMEs.

    Reply

    • Is that a recent change to the API? Because when I wrote this, it had no problem creating CNAMEs. That’s the Best Practice for anything within Amazon’s cloud, as communication between the boxes resolves to the internal IP address when using the CNAME.

      Cloudfront also requires the use of CNAMEs or Aliases (internal to Route53). 

      Reply

      • A Round robin of CNAMEs is illegal according to rfc2181:
        “[…] That is, for any label in the DNS (any domain name)
        exactly one of the following is true:
        + one CNAME record exists, optionally accompanied by SIG, NXT, and
        KEY RRs, […]”

        I just tried to create a round robin of CNAMEs on Route53 and got the expected response:

        “RRSet of type CNAME with DNS name testcname.mydomain. does not contain exactly one resource record.”

        Either use a RR of IP addresses or do a weighted round robin of CNAMEs, all with the same weight (see http://docs.amazonwebservices.com/Route53/latest/DeveloperGuide/WeightedResourceRecordSets.html for more info on Weighted records).

        Your script tried to create a “simple” (i.e. regular) round robin and therefore it fails.
        I just replaced “CNAME” with “A” at line 77 and public-hostname with public-ipv4 at line 27 to make it work.

        Reply

        • That’s partly why Route53 does Aliases. When doing a round-robin, you need to make it weighted. 

          Reply

          •  Just a word of advice: I was fooled into thinking that the script doesn’t work because when resolving a weighted round robin, Route53 only returns one CNAME (in rotation), not the whole set.
            So don’t be surprised if you start N instances and when you try to resolve the round robin you get only 1 IP address at a time.
            If you want to have all the IPs in a single record you have to use an A record.

          • That’s a good point. If you want it to return all the records and have the client pick, you’ll definitely need an A record. 

            The latency-weighted stuff is pretty cool for geographical load balancing, too. 

Leave a Reply

Your email address will not be published. Required fields are marked *