In this post, you will find how to update AWS Route53 from your current public IP (ISP) and how to use EC2 instance metadata to change your DNS A Record.
Requirements#
To use this script, you will need:
awscli (pip install awscli)
dig (apt-get install dnsutils)
Update Route53 with your current home Public IP#
First, I was facing an issue with my current ISP with a non-static IP address, so I found a useful script to update AWS Route53 daily from my Raspberry Pi.
Please find the how-to on my GitHub.
I didn’t want to set up an ALB/ELB because it cost too much for my needs (labs). So I’ve adapted this article to meet my needs.
Follow the same steps and adapt Update53-EC2Pub.sh and UserData from my Launch Configuration below:
update-route53-A.json & Update53-EC2Pub.sh#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
{
"Comment": "Update the A record set",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "MY_DNS_RECORD_NAME",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "127.0.0.1"
}
]
}
}
]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/sh
if [ -z "$1" ]; then
echo "IP not given...trying EC2 metadata...";
IP=$( curl -s http://169.254.169.254/latest/meta-data/public-ipv4 )
else
IP="$1"
fi
echo "IP to update: $IP"
HOSTED_ZONE_ID=$( aws route53 list-hosted-zones-by-name | grep -B 1 -e "YOUR_PARENT_DOMAIN_NAME" | sed 's/.*hostedzone\/\([A-Za-z0-9]*\)\".*/\1/' | head -n 1 )
echo "Hosted zone being modified: $HOSTED_ZONE_ID"
INPUT_JSON=$( cat /home/ec2-user/update53/update-route53-A.json | sed "s/127\.0\.0\.1/$IP/" )
# http://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html
# We want to use the string variable command so put the file contents (batch-changes file) in the following JSON
INPUT_JSON="{ \"ChangeBatch\": $INPUT_JSON }"
aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$INPUT_JSON"
|
UserData#
In this example, I’ve compressed and hosted the source in a simple S3 bucket, and use it with the instance UserData.
1
2
3
4
5
6
|
#!/bin/bash
/usr/bin/aws s3 cp s3://YOU_S3_BUCKET/update53.tar.bz2 /home/ec2-user/
tar xjvf /home/ec2-user/update53.tar.bz2 -C /home/ec2-user/
rm /home/ec2-user/update53.tar.bz2
/bin/sh /home/ec2-user/update53/update.sh >> /home/ec2-user/update53.log
rm -rf /home/ec2-user/update53/
|
It’s working pretty well. With scheduled actions on my ASG, my instance shuts down every night and fires up the next morning, and the public IP is automatically updated in my AWS Route53 A Record.
Don’t hesitate to ping me with any question.
That’s all folks!
zoph.