Skip to content

Instantly share code, notes, and snippets.

@anthonyheddings
Last active January 20, 2023 13:10
Embed
What would you like to do?
#!/bin/bash
#Variable Declaration - Change These
HOSTED_ZONE_ID="XXXXXXXX"
NAME="example.com."
TYPE="A"
TTL=60
#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
aws2 route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID | \
jq -r '.ResourceRecordSets[] | select (.Name == "'"$NAME"'") | select (.Type == "'"$TYPE"'") | .ResourceRecords[0].Value' > /tmp/current_route53_value
cat /tmp/current_route53_value
#check if IP is different from Route 53
if grep -Fxq "$IP" /tmp/current_route53_value; then
echo "IP Has Not Changed, Exiting"
exit 1
fi
echo "IP Changed, Updating Records"
#prepare route 53 payload
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
#update records
aws2 route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch file:///tmp/route53_changes.json >> /dev/null
@cleitonpena
Copy link

Hello, Anthony
Thank you for this script. It's very useful.
Can you help me?
I have this issue:
update_dns.sh: line 18: jq: command not found

The version awscli in Ubuntu 20.04 is
aws-cli/2.5.3 Python/3.9.11 Linux/5.13.0-1021-aws exe/x86_64.ubuntu.20 prompt/off

But the aws2 command doesn't work. I switched to aws and was able to fix it.

@FullStackIndie
Copy link

FullStackIndie commented Nov 29, 2022

@cleitonpena I don't know if you ever figured it out but will post here for future viewers.

You gotta Install jq on ubuntu

sudo apt update
sudo apt install -y jq

When installation is finished, check jq version:
jq --version

source article - https://lindevs.com/install-jq-on-ubuntu/

For future readers

I was having problems with jq in my .sh script but finally found a solution on stack overflow,
there was a problem with the shell variables but by escaping them like \"$NAME\"" it finally worked.

I was having problems with /tmp folder access being denied so I added all the files under my user murph - /home/murph

I think aws2 was a preview AWS CLI package but I changed mine to just aws route53

My cronjob is running on root user as well - use sudo crontab -e to make it a root cronjob
Also had to run aws configure on user root as well as user murph
I used sudo in my script to avoid problems creating a log file in /var/log and because cronjob is on root it may overkill but put just in case

Also, I added more websites to mine that will be changed.

My version here - gist

@seth586
Copy link

seth586 commented Jan 12, 2023

On FreeBSD, aws-cli version aws-cli/1.20.61 Python/3.9.16, the command

aws route53 list-resource-record-sets

does not return a JSON, but rather plain text, example:

RESOURCERECORDSETS      domain.com. 60      A
RESOURCERECORDS 54.163.55.155
RESOURCERECORDSETS      domain.com. 300     MX
RESOURCERECORDS 10 aspmx1.migadu.com
RESOURCERECORDS 20 aspmx2.migadu.com
RESOURCERECORDSETS      domain.com. 172800  NS
...

jq wont parse, so I replaced line 19 with grep:

grep -m1 -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > /tmp/current_route53_value

which just returns the first IP address found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment