Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active June 3, 2024 13:03
Show Gist options
  • Save slavafomin/48c5ea5bd1bb75be75fb441fadd9cd9d to your computer and use it in GitHub Desktop.
Save slavafomin/48c5ea5bd1bb75be75fb441fadd9cd9d to your computer and use it in GitHub Desktop.
AWS EC2 Dynamic DNS

AWS EC2 Dynamic DNS

Use the provided script to update your Route 53 domain name to point to the EC2 instance IP address that changes dynamically on reboot.

# configure crontab
sudo -i
chmod ug=rx,o= /root/update-dns.sh
systemctl enable cron.service
crontab -e
# crontab file
@reboot /root/update-dns.sh
#!/bin/bash
HOSTED_ZONE_ID=""
NAME="example.com."
TYPE="A"
TTL=60
IP=$(curl http://checkip.amazonaws.com/)
if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then
echo "Failed to obtain IP address"
exit 1
fi
echo "Updating A-record to point to: $IP"
cat > /tmp/route53_changes.json << EOF
{
"Comment":"Updated From DDNS Shell Script",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"ResourceRecords": [{ "Value":"$IP" }],
"Name": "$NAME",
"Type": "$TYPE",
"TTL": $TTL
}
}
]
}
EOF
aws route53 change-resource-record-sets \
--hosted-zone-id $HOSTED_ZONE_ID \
--change-batch file:///tmp/route53_changes.json >> /dev/null
echo "Done"
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:DescribeTags",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "route53:ChangeResourceRecordSets",
"Resource": "arn:aws:route53:::hostedzone/Z310AKR6T33BBU"
},
{
"Effect": "Allow",
"Action": "route53:ChangeResourceRecordSets",
"Resource": "arn:aws:route53:::hostedzone/Z0466444CI3IIHSFDP5"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment