Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Last active November 30, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apolloclark/605c82b2fcfadebb58c74d4dd76a5a15 to your computer and use it in GitHub Desktop.
Save apolloclark/605c82b2fcfadebb58c74d4dd76a5a15 to your computer and use it in GitHub Desktop.
Dynamically update an AWS Route53 sub-domain IP address to your home router IP
[change-resource-record-set.json]
{
"Comment": "Updating IP address",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "domain.example.com",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "127.0.0.1"
}
]
}
}
]
}
[update_ipv4.sh]
#!/bin/bash -eux
# retrieve the local public ip, and domian IP
export domain="domain.example.com"
export home_ipv4="$(curl -s http://whatismyip.akamai.com)";
export site_ipv4="$(dig +short $domain)";
# check if they match
if [ "$home_ipv4" == "$site_ipv4" ]
then
# log the match, exit
logger "$domain IP already matches"
exit 0;
fi
# log the mismatch
logger "updating IP address for $domain to $home_ipv4"
# retrieve hostedZoneId
hostedZoneId=$(aws route53 list-hosted-zones \
--query 'HostedZones[?Config.PrivateZone==`false`].Id' \
--output text \
| cut -d'/' -f3 );
echo "$hostedZoneId";
# retrieve the JSON file, change value as needed, save results
changeBatch=$({
jq '.Changes[].ResourceRecordSet.ResourceRecords[0].Value=env.home_ipv4'
} < change-resource-record-set.json);
echo "$changeBatch";
# update the record
# https://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html
recordChangeResult=$(aws route53 change-resource-record-sets \
--hosted-zone-id "$hostedZoneId" \
--change-batch "$changeBatch");
echo "$recordChangeResult";
exit 0;
# update crontab, to run the script every 5 min
crontab -e
*/5 * * * * ~/update_ipv4.sh
# check the syslog to verify the script is running
grep -F "domain.example.com" /var/log/syslog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment