Skip to content

Instantly share code, notes, and snippets.

@lantrix
Forked from TonyFNZ/dnsupdate.sh
Last active March 1, 2022 18:39
Show Gist options
  • Save lantrix/3f8fbe8c8b18469d0472279634d90fb9 to your computer and use it in GitHub Desktop.
Save lantrix/3f8fbe8c8b18469d0472279634d90fb9 to your computer and use it in GitHub Desktop.
Script to update Route53 with the current public IP of an instance
#!/bin/bash
hosted_zone_id='Z57Q212345678'
domain_name='server.example.com'
# Abort script on any errors
set -e
# Get new IP address
ip_address=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
# Build temporary file
cat > ./dnsupdate.json <<EOF
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${domain_name}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "${ip_address}"
}
]
}
}
]
}
EOF
# Call Route53 to update DNS
aws route53 change-resource-record-sets --hosted-zone-id ${hosted_zone_id} --change-batch file://./dnsupdate.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment