Auto-Register EC2 Instance in AWS Route 53

The Challenge

In this blogpost I would like to show you, how an EC2 instance can register itself automatically on AWS Route 53. This could be important, if you have ie. a clustered group of nodes (like an Apache ZooKeeper or Kafka Cluster) where a list of DNS-Names or IP-Addresses need to be configured and you don’t want to use AWS specific DNS names.It is very easy to start new instances or make a copies of amazon machine images, but if you have a fixed set of nodes with fixed addresses you have to solve the challenge, that the private and even the elastic and public accessible IP’s will be different if you start a new machine generated from an image. You do not want to change the configuration afterwards only because you started a new machine.

In the early days we could achive that through different ways. Sometimes I used ie. DynDNS for self healing DNS entries or a fixed IP, that was assigned through a DNS-Gateway based on the machines MAC address. In AWS this is not that easy, because a newly created machine should have a different physical address. You can still use DynDNS, but then you can not use the other features of AWS Route 53.

The Steps

  1. Create a private hosted zone in AWS Route 53 (you can use whatever you like as domain name)
  2. Create a new AWS IAM Policy (for a restricted Route 53 DNS Access)
  3. Create a new AWS IAM Group
  4. Create a new AWS IAM User
  5. Create a new AWS EC2 instance and add some predefined tags
  6. Installation of some tools and a script to register the instance on startup or manually on AWS Route 53

1) Create the private hosted zone

First login to AWS Console and switch to Route 53 service. Create a new private hosted zone. The same steps are also possible with a public zone. I used as my domain so that we later have several nodes called ie., Remember the ID from the created zone (in my case Z2IZ9V712F87LJ) because we will use this later when we create A-Records in our Route 53 zone. The hosted zone is normaly attached to a AWS Virtual Private Cloud Network, so later instances need to be part of the VPC to change the DNS tables.

Create a private or public hosted zone in AWS Route 53

2) Create a new AWS IAM Policy for a restricted Route 53 DNS Access

Next we need to create a new IAM Policy. Therefore switch to the IAM service. With this custom policy we like to allow access to read or change the record sets and to the hosted zone itself. Here we use the zone ID to specify the Amazon Resource Name (ARN) that this rules will apply to. The policy should also allow us to list all hosted zones.

Create a policy that allow access to the zone

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "Stmt1492592612000",
            "Effect": "Allow",
            "Action": [
            "Resource": [
            "Action": [
            "Effect": "Allow",
            "Resource": [

3) Create a new AWS IAM Group

After the policy is ready to use we can create a new group. Assign the new policy to the group as well you can add the default ec2 read only access policy. If you like you can create a custom policy too for specific resources, for my case this policy is restrictive enough.

Create a group with the new policy and read access to EC2

4) Create a new AWS IAM User

The last we need to prepare is a new user that has only programmatic access via command line tools. We need the credentials of this technical user later to enable the access from the instances to AWS Route 53. In step 2 of the wizzard assign the user to the group we created.

Create a new user with programatic access that is assigned to the group

5) Create a new AWS EC2 instance and add some predefined tags

Next we will create some EC2 instances. In the tags view you can see the two additional tags internal-hostname and public-hostname. We will use this two tags later to register the A-Record at Route 53 to that names. If you don’t like to use AWS tags, you can also write the hostname to instance startup as environment variable or into another script. I like the way to solve this with tags because it’s very transparent and easy to use.

Add the internal and public hostname tag to the instance

6) Installation of some tools and a script to register the instance on startup or manually on AWS Route 53

Now connect via ssh to your EC2 machine with your key and start the preparation of the instance. First we install python and the AWS client tools.

sudo apt-get update
sudo apt-get install -y python-pip
sudo pip install -U pip
sudo pip install awscli

Go to your home directory and download a tool that is available on github (here). This allows us to add, change or remove records form Route 53 through command line calls.

sudo mv cli53-linux-amd64 /usr/local/bin/cli53
sudo chmod +x /usr/local/bin/cli53

Add a new file that contains the credentials for the AWS client tools and also the ID from the hosted zone.

sudo mkdir /etc/route53
sudo chmod 700 /etc/route53
sudo touch /etc/route53/config
sudo chmod 600 /etc/route53/config
sudo nano /etc/route53/config

Save the following constants to the config file.

# AWS Credentials used by AWS CLI and CLI53

# The ID of our AWS Route53 Zone

Then create the file that contains the script to update the AWS Route 53 records.

sudo touch /usr/sbin/update-route53-dns
sudo chmod +x /usr/sbin/update-route53-dns
sudo nano /usr/sbin/update-route53-dns

In the following script we load the config file and make the AWS access key and secret key accessible. Then we will discover the AWS region where the instance is hosted in. If you are confused about the public IP address that is used in the curl command, please read here for more information. Then we gather the instance-id from ec2metadata. With aws ec2 describe-tags we can return all tags that are assigne to the instance. Feel free to change the name of the tags. Now we just need the private and public IP-Addresses of the instance. Caution, I configured my VPC, that all new instances get a Elastic IP automatically. If you do not assign a public EIP just delete the three lines that are related to the public IP and hostname.


# Load configuration and export access key ID and secret for cli53 and aws cli
. /etc/route53/config

# The TimeToLive in seconds we use for the DNS records 

# Get the private and public hostname from EC2 resource tags
REGION=$(curl -s | grep region | awk -F" '{print $4}')
INSTANCE_ID=$(ec2metadata | grep 'instance-id:' | cut -d ' ' -f 2)
INTERNAL_HOSTNAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=internal-hostname" --region=$REGION --output=text | cut -f5)
PUBLIC_HOSTNAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=public-hostname" --region=$REGION --output=text | cut -f5)

# Get the local and public IP Address that is assigned to the instance
LOCAL_IPV4=$(ec2metadata | grep 'local-ipv4:' | cut -d ' ' -f 2)
PUBLIC_IPV4=$(ec2metadata | grep 'public-ipv4:' | cut -d ' ' -f 2)

# Create a new or update the A-Records on Route53 with public and private IP address
cli53 rrcreate --replace "$ZONE" "$INTERNAL_HOSTNAME $TTL A $LOCAL_IPV4"
cli53 rrcreate --replace "$ZONE" "$PUBLIC_HOSTNAME $TTL A $PUBLIC_IPV4"

Add the following line to /etc/rc.local so that the script get’s executed on restart.

sudo nano /etc/rc.local

# Add this line before exit 0

exit 0

To test the script just log in as root user and execute the script.

sudo su -

Now open up Route 53 in AWS Console. You should see that all three of my test instances added two A-Records each. The first ( can be used if a request should be routed over public internet and the second ( can be used if two instances should communicate between each other without using public internet.


You can also try to communicate between the servers if you install tcptraceroute. Call ie. tcptraceroute 22 from server 1 to prove if the route is solvable and reachable without hops over some public routers.

Unsere Leistungen im Überblick

Mehr erfahren