Scripting updates to AWS Route 53 Hosted Zone entries using CLI

Scripting updates to AWS Route 53 Hosted Zone entries using CLI

In this post I describe how to use the Amazon Command Line Interface (CLI) to update Route 53 Hosted Zone entries. When you shutdown and start up an AWS instance it will be assigned a new IP address. Of course this is only when you actually shutdown the instance, just doing a restart will maintain the existing address. Unfortunately assigning a new IP address will mean that your Route 53 Hosted Zone will be pointing to an old address. For larger sites you can set up an Elastic Load Balancer (ELB) which can be configured to automatically check for and/or start up new instances but for smaller, hobbyist sites an ELB is often over the top and can be quite expensive.

Luckily there’s a vary easy way to update the Hosted Zone entries using the Amazon command line tools.

To begin you need to install the Amazon command line tools (CLI). If you’re running an Amazon Linux instance they should be already installed for you. If not follow these instructions http://docs.aws.amazon.com/cli/latest/userguide/installing.html.

Before you start you will need to get two bits of information from your Route 53 Hosted Zone entries. You can get these by logging into the AWS console and viewing your Hosted Zone entries.

The first one is the ID of the Hosted Zone you are going to update. You can find this on the

locate hosted zone id

The second item is the A-record of the Hosted Zone which you want to update. Note that if you have a www A-record as well you will need to update each one separately.

locate hosted zone A record

Armed with these two pieces of information you’re ready to start doing the update. Updating Route 53 Hosted Zone entries involves passing in a json file with the changes. Below is a template for the change. Note that we’re doing an UPSERT action as we’ll be updating an existing entry.

53_template.json

{
  "Comment": "A new record set for the zone.",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": “DNSNAME",
        "Type": "A",
        "TTL": 86400,
        "ResourceRecords": [
          {
            "Value": “xx.xx.xx.xx"
          }
        ]
      }
    }
  ]
}

The template file contains two parameters which we will change before issuing it. These are the DNS name of the record you want to update (DNSNAME) and the new IP address of the server (xx.xx.xx.xx).

To set these values I’ve created a script file called update53.sh which is shown below. The script starts by taking a copy of the json template file which will be edited with the actual values we need to pass in.

You’ll need to replace the values for id and dns with the information you got from the Route 53 console. The script then reads the public ip address of the server and replaces all the values in the json template file.

Finally it calls the Amazon CLI to update the Route 53 Hosted Zone record using the ID and json file. The updates will go into a queue so can take a few seconds to apply.

update53.sh

#Replace these with your values
id=XXXXXXXXXXXX
dns=hamishbuchanan.com

cp 53_template.json /tmp/53.json
new_ip=`sudo ec2metadata --public-ipv4`


sed -i -- "s/xx.xx.xx.xx/${new_ip}/g" /tmp/53.json
sed -i -- "s/DNSNAME/${dns}/g" /tmp/53.json

cd /tmp
aws route53 change-resource-record-sets --hosted-zone-id $id --change-batch file://53.json

So there you have it a simple script to update Route 53 Hosted Zone entries and point them to the public ip address of the current server. It’s certainly not as sophisticated as an Elastic Load Balancer and would need more work to be automated but for simple sites where an ELB is over the top it does the job.