Skip to content

Instantly share code, notes, and snippets.

@brandon1024
Last active May 13, 2022 23:34
Show Gist options
  • Save brandon1024/79f943fb942af6d9d06545e09b676722 to your computer and use it in GitHub Desktop.
Save brandon1024/79f943fb942af6d9d06545e09b676722 to your computer and use it in GitHub Desktop.
The Dumbest Dynamic DNS [Route53]

A Dumb Route53 Dynamic DNS Solution

There's really not much to it.

You'll need an AWS Route53 hosted zone and a suitable AWS user. Install and configure AWS CLI. Update ddns-sync.sh variables (DNS_NAME and HOSTED_ZONE_ID) appropriately. Then,

$ cp ddns-sync.sh      ${HOME}
$ cp ddns-sync.service ${HOME}/.config/systemd/user/
$ cp ddns-sync.timer   ${HOME}/.config/systemd/user/
$ systemctl --user enable ddns-sync.service
$ systemctl --user enable ddns-sync.timer
$ systemctl --user start  ddns-sync.timer

If your host's public IP changes, the script will update the DNS route.

Enjoy 😊

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:ChangeResourceRecordSets"
],
"Resource": [
"arn:aws:route53:::hostedzone/<YOUR HOSTED ZONE ID>"
]
}
]
}
[Unit]
Description=Sync an AWS Route53 DNS Alias Record to Public Address of Current Host
[Service]
Type=oneshot
ExecStart=%h/ddns-sync.sh
[Install]
WantedBy=default.target
#!/usr/bin/env bash
DNS_NAME=<YOUR DNS NAME>
HOSTED_ZONE_ID=<YOUR HOSTED ZONE ID>
RESOLV_ADDR=($(getent hosts ${DNS_NAME}))
RESOLV_ADDR="${RESOLV_ADDR[0]}"
PUBLIC_IP="$(curl -s https://checkip.amazonaws.com)"
if [[ "${RESOLV_ADDR}" == "${PUBLIC_IP}" ]]; then
exit 0
fi
echo "Upserting DNS alias record ${DNS_NAME} in hosted zone ${HOSTED_ZONE_ID} from ${RESOLV_ADDR} to ${PUBLIC_IP}"
aws route53 change-resource-record-sets --hosted-zone-id ${HOSTED_ZONE_ID} --change-batch "
{
\"Changes\": [{
\"Action\": \"UPSERT\",
\"ResourceRecordSet\": {
\"Name\": \"${DNS_NAME}\",
\"Type\": \"A\",
\"TTL\": 900,
\"ResourceRecords\": [{
\"Value\": \"${PUBLIC_IP}\"
}]
}
}]
}"
[Unit]
Description=Sync an AWS Route53 DNS Alias Record to Public Address of Current Host
[Timer]
OnBootSec=5min
OnUnitActiveSec=10min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment