pdp.dev

Route 53 DNS Updater

I use Route 53 for my DNS and have an A record that points to home for Wireguard access. Here’s a handy script that I run in cron to keep that record updated.

#!/bin/sh

### Things you need to set
###
# The record to update
DNS_RECORD="blahblah.com"
# The AWS profile to use for creds, must exist in ~/.aws/credentials
PROFILE_NAME="yr-creds"
# Your hosted zone ID
ZONE_NAME="yr-zone-id"
###
### END Things you need to set

IP="$(curl -s http://checkip.amazonaws.com/)"
TMP_FILE="$(mktemp)"

cat > "${TMP_FILE}" << EOF
{
  "Comment": "DDNS update",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "ResourceRecords": [
          {"Value": "${IP}"}
        ],
        "Name": "${DNS_RECORD}",
        "Type": "A",
        "TTL": 300
      }
    }
  ]
}
EOF

aws --profile ${PROFILE_NAME} route53 change-resource-record-sets \
    --hosted-zone-id "/hostedzone/${ZONE_NAME}" \
    --change-batch "file://${TMP_FILE}"

Then I run it in cron every 20 minutes, which you set using crontab -e.

# m h  dom mon dow   command
*/20   *    *   *   *   bash /path/to/script/ddns.sh >> /path/to/output/ddns.out