Last active
February 21, 2023 23:58
-
-
Save FullStackIndie/0eb78c31a8a7d1c31e8d944b1bd68171 to your computer and use it in GitHub Desktop.
Updated Ip-Check - Custom Dynamic DNS for AWS Route 53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Variable Declaration - Change These | |
HOSTED_ZONE_ID="Z0244******" | |
#test/dummy subdomain to see if my IP has changed | |
NAME="dynamic-dns.*********.net." | |
#My websites that need there IP address updated | |
CRITTER="development.*********.net." | |
IDENTITY="development.*********.net." | |
GATEWAY="development.*********.net." | |
TYPE="A" | |
#Using 60 for the health check to prevent long DNS caching time | |
HEALTH_CHECK_TTL=60 | |
#Using 300 for my websites - at most my websites won't work for 5 minutes (browser may cache DNS entries) but AWS ROUTE 53 updates #DNS pretty fast | |
TTL=300 | |
#Get current IP address | |
IP=$(curl http://checkip.amazonaws.com/) | |
#validate IP address (makes sure Route 53 doesn't get updated with a malformed payload) | |
if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then | |
exit 1 | |
fi | |
#get current ip - uses AWS query option for server side filtering instead of client-side using jq | |
# --query uses JMESPath syntax. similar to jq | |
# https://jmespath.org/tutorial.html | |
aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --profile home-server \ | |
--query "ResourceRecordSets[?Name == 'dynamic-dns.fullstackindie.net.'].ResourceRecords[0].Value" --output text > /home/murph/workspace/cron-jobs/current_route53_value | |
# add file to keep track of last update time | |
LAST_UPDATE_TIME=$(date --iso-8601=seconds) | |
cat > /home/murph/workspace/cron-jobs/cron_updates << EOF | |
$LAST_UPDATE_TIME | |
EOF | |
#check if IP is different from Route 53 | |
if grep -Fxq "$IP" /home/murph/workspace/cron-jobs/current_route53_value; then | |
echo "IP Has Not Changed, Exiting" | |
exit 1 | |
fi | |
echo "IP Changed, Updating Records" | |
#prepare route 53 payload | |
cat > /home/murph/workspace/cron-jobs/route53_changes.json << EOF | |
{ | |
"Comment":"Updated From DDNS Shell Script", | |
"Changes":[ | |
{ | |
"Action":"UPSERT", | |
"ResourceRecordSet":{ | |
"ResourceRecords":[ | |
{ | |
"Value":"$IP" | |
} | |
], | |
"Name":"$NAME", | |
"Type":"$TYPE", | |
"TTL":$HEALTH_CHECK_TTL | |
} | |
}, | |
{ | |
"Action":"UPSERT", | |
"ResourceRecordSet":{ | |
"ResourceRecords":[ | |
{ | |
"Value":"$IP" | |
} | |
], | |
"Name":"$CRITTER", | |
"Type":"$TYPE", | |
"TTL":$TTL | |
} | |
}, | |
{ | |
"Action":"UPSERT", | |
"ResourceRecordSet":{ | |
"ResourceRecords":[ | |
{ | |
"Value":"$IP" | |
} | |
], | |
"Name":"$IDENTITY", | |
"Type":"$TYPE", | |
"TTL":$TTL | |
} | |
}, | |
{ | |
"Action":"UPSERT", | |
"ResourceRecordSet":{ | |
"ResourceRecords":[ | |
{ | |
"Value":"$IP" | |
} | |
], | |
"Name":"$GATEWAY", | |
"Type":"$TYPE", | |
"TTL":$TTL | |
} | |
} | |
] | |
} | |
EOF | |
#update records | |
sudo aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch file:///home/murph/workspace/cron-jobs/route53_changes.json >> /var/log/ip-check.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment