How to update Route53 records after EC2 instance restart

Amazon Route 53 is a highly available and scalable cloud Domain Name System (DNS) web service. If you are not using Elastic IPs for your EC2 instances, chances are stopping and starting the server will result in different IPs after the instance comes back online. If you have A records pointing to those IPs in Route53 you will need a way to update them. After the script is executed, it will automatically gather the new Public IP and update the DNS record for it in Route53.

Edit 1:

As I am getting many comments on hosted zone parsing error… I wanted to add this

Before running script please check your host name is set to fully qualified domain name (FQDN) with this command
hostname -f

or

hostname -d

or

hostnamectl

If you don’t want to set host name system wide, feel free to modify the script manually

*note: use set -ex option to debug the script

19 thoughts on “How to update Route53 records after EC2 instance restart”

  1. [ec2-user@ip-172-31-22-106 ~]$ sudo ./ipregister.sh
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 12 0 12 0 0 28 0 –:–:– –:–:– –:–:– 28
    Unable to locate credentials. You can configure credentials by running “aws configure”.
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –hosted-zone-id: expected one argument
    Updating resource record set

    {
    “Comment”: “Update record to reflect new public IP address”,
    “Changes”: [
    {
    “Action”: “UPSERT”,
    “ResourceRecordSet”: {
    “Name”: “ip-172-31-22-106.ap-south-1.compute.internal.”,
    “Type”: “”,
    “TTL”: 300,
    “ResourceRecords”: [
    {
    “Value”: “35.154.76.60”
    }
    ]
    }
    }
    ]
    }
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –hosted-zone-id: expected one argument
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –id: expected one argument
    Record updated!
    —————————————–
    Please help me with the error, Akhil

  2. This is a really nice script. Can you please explain the minimum permissions it would require to allow access using an EC2 role instead of adding credentials to the EC2 instance.

  3. Hello AKhil,
    Nice script. Will this work for private ip of ec2 as well. I have configured multiple auto scaling groups. The instance increases and decreases as per load. So what i want to do is When an instance spins up, record should get added in route53 and when ec2 gets terminated the record for that instance should get deleted .

    1. Yes. It works for private IP also. You need to replace PUBLIC_IP=$(curl ident.me) with PUBLIC_IP=$(hostname -I) so it will get private IP.

  4. Hai , i am getting the below error .. please help me

    [root@test ~]# sh /opt/test.sh
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 14 100 14 0 0 30 0 –:–:– –:–:– –:–:– 30
    parse error: Invalid numeric literal at line 1, column 12
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –hosted-zone-id: expected one argument
    Updating resource record set

  5. Hai Akhil,

    still same error

    [root@test opt]# bash autoupdateip_route53.sh
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 14 100 14 0 0 34 0 –:–:– –:–:– –:–:– 34
    parse error: Invalid numeric literal at line 1, column 12
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –hosted-zone-id: expected one argument
    Updating resource record set

    {
    “Comment”: “Update record to reflect new public IP address”,
    “Changes”: [
    {

    Please help me

  6. errror:

    parse error: Invalid numeric literal at line 1, column 12

    what it means where i need to check this

  7. ++ hostname -f
    + HOSTNAME=test.example.xyz
    ++ curl ident.me
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 14 100 14 0 0 34 0 –:–:– –:–:– –:–:– 34
    + PUBLIC_IP=13.232.230.116
    ++ awk -F. ‘{print $(NF-2)”.”$(NF-1)”.”$(NF)}’
    ++ hostname -d
    + HOSTED_ZONE=example.xyz.example.xyz
    ++ tr -d ‘”‘
    ++ awk -F/ ‘{print $3}’
    ++ jq –arg hosted_zone example.xyz.example.xyz. ‘.HostedZones[] | select(.Name == $hosted_zone) | .Id’
    ++ aws route53 list-hosted-zones
    parse error: Invalid numeric literal at line 1, column 12
    + ZONE_ID=
    ++ tr -d ‘”‘
    ++ jq ‘.[].Type’
    ++ aws route53 list-resource-record-sets –hosted-zone-id –query ‘ResourceRecordSets[?Name == ‘\”test.example.xyz.’\”]’
    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    in hosted_zone two times domain name is comming

    1. your ~/.aws/config should look like this

      [profile ]
      output = json
      region =

      or add –output json to the aws commands inside script

  8. Hai Akhil,
    Thank you somuch, Parse error issue resolved…

    But still this is there…

    HOSTED_ZONE=$(hostname -d | awk -F”.” ‘{print $(NF-2)”.”$(NF-1)”.”$(NF)}’)

    For the above script :
    example.xyz.example.xyz

    Please help

  9. The problem is:

    Variable ZONE_ID is empty. That’s throwing the following error:

    usage: aws [options] [ …] [parameters]
    To see help text, you can run:

    aws help
    aws help
    aws help
    aws: error: argument –hosted-zone-id: expected one argument

    Any hints?

Leave a Reply

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