Skip to content

Instantly share code, notes, and snippets.

@Sleepdealr
Last active April 5, 2022 20:56
Show Gist options
  • Save Sleepdealr/59d94ce36a5a4e8826cb231ad9cef190 to your computer and use it in GitHub Desktop.
Save Sleepdealr/59d94ce36a5a4e8826cb231ad9cef190 to your computer and use it in GitHub Desktop.
Update Route53's IP to the server's current IP
#!/bin/bash
ZONE_ID="[REDACTED]"
NAME="[REDACTED]"
AWS_IP=`aws route53 list-resource-record-sets --hosted-zone-id $ZONE_ID | jq '(.ResourceRecordSets[] | select(.Name | test("'$NAME'")) | [.ResourceRecords] )' | jq -r '.[0][0].Value'`
RETVAL=$?
if [ $RETVAL -ne 0 ]; then # Check if exit code doesn't equal 0
exit 0
fi
echo "AWS IP: $AWS_IP"
CUR_IP=`curl -s icanhazip.com`
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
exit 0
fi
echo "CURRENT IP: $CUR_IP"
# If current IP is equal to the AWS IP, we don't need to change anything
if [ "$CUR_IP" = "$AWS_IP" ]; then
exit 0
fi
echo ' { ' > insert_rulset.json
echo ' "HostedZoneId" : "'$ZONE_ID'", ' >> insert_rulset.json
echo ' "ChangeBatch": { ' >> insert_rulset.json
echo ' "Comment": "", ' >> insert_rulset.json
echo ' "Changes": [ ' >> insert_rulset.json
echo ' { ' >> insert_rulset.json
echo ' "Action": "UPSERT", ' >> insert_rulset.json
echo ' "ResourceRecordSet": { ' >> insert_rulset.json
echo ' "Name": "'$NAME'", ' >> insert_rulset.json
echo ' "Type": "A", ' >> insert_rulset.json
echo ' "TTL": 3600, ' >> insert_rulset.json
echo ' "ResourceRecords": [ ' >> insert_rulset.json
echo ' { ' >> insert_rulset.json
echo ' "Value": "'$CUR_IP'" ' >> insert_rulset.json
echo ' } ' >> insert_rulset.json
echo ' ] ' >> insert_rulset.json
echo ' } ' >> insert_rulset.json
echo ' } ' >> insert_rulset.json
echo ' ] ' >> insert_rulset.json
echo ' } ' >> insert_rulset.json
echo ' } ' >> insert_rulset.json
aws route53 change-resource-record-sets --cli-input-json file://insert_rulset.json
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "Updated AWS IP successfully"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment