Managing dynamic IP addresses can be tricky if you host services at home or in an environment where your public IP changes periodically. Instead of manually updating DNS records every time your IP changes, you can automate the process with AWS Route 53 and a simple cron job running on a RHEL-based server.

In this post, I’ll walk through how I keep my Route 53 DNS entry updated automatically.

Why Route 53 and Cron?

AWS Route 53 is a highly available and scalable DNS service. It works perfectly for custom domains, but AWS doesn’t provide a built-in dynamic DNS client like some other providers do.

To solve this, I use a simple Bash script that:

  1. Fetches my current public IP.
  2. Compares it to the IP in my Route 53 hosted zone record.
  3. Updates the record if my IP has changed.

Then, I schedule this script to run periodically using cron on my RHEL server.

Prerequisites

  • An AWS account with Route 53 configured and a hosted zone for your domain.
  • AWS CLI installed and configured on your RHEL server (aws configure).
  • A record set (A record) in Route 53 that you want to update.

The Script

Here’s the basic Bash script I use (update-route53-dns.sh):

#!/bin/bash

HOSTED_ZONE_ID="Z123456789EXAMPLE"
RECORD_NAME="home.example.com"
TTL=300

CURRENT_IP=$(curl -s https://checkip.amazonaws.com)
OLD_IP=$(aws route53 list-resource-record-sets \
  --hosted-zone-id $HOSTED_ZONE_ID \
  --query "ResourceRecordSets[?Name == '$RECORD_NAME.'].ResourceRecords[0].Value" \
  --output text)

if [ "$CURRENT_IP" != "$OLD_IP" ]; then
  echo "IP changed from $OLD_IP to $CURRENT_IP. Updating Route 53..."
  cat > /tmp/route53.json <<EOF
{
  "Comment": "Auto-updated by cron",
  "Changes": [{
    "Action": "UPSERT",
    "ResourceRecordSet": {
      "Name": "$RECORD_NAME",
      "Type": "A",
      "TTL": $TTL,
      "ResourceRecords": [{ "Value": "$CURRENT_IP" }]
    }
  }]
}
EOF

  aws route53 change-resource-record-sets \
    --hosted-zone-id $HOSTED_ZONE_ID \
    --change-batch file:///tmp/route53.json
else
  echo "IP address has not changed ($CURRENT_IP)."
fi

Make it executable:

chmod +x update-route53-dns.sh

Setting Up the Cron Job

To schedule the script to run every 10 minutes, edit the cron table:

crontab -e

Add:

*/10 * * * * /path/to/update-route53-dns.sh >> /var/log/route53-update.log 2>&1

This ensures your DNS record is always up-to-date without manual intervention.

Similar Posts