update_r53.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/sh
  2. #set -x
  3. ### UPDATE THESE
  4. HOSTNAME='myrouter.mydomain.com'
  5. HOSTEDZONE='myhostedzoneID'
  6. ### NO NEED TO EDIT BELOW THIS LINE
  7. function update {
  8. logger "Route53 UPDATE: ${1} -> ${2}"
  9. # location of a virtualenv with awscli configured with Route53 permissions
  10. . $HOME/awscli/bin/activate
  11. cat << _EOF > /tmp/${1}
  12. {
  13. "Changes": [
  14. {
  15. "Action": "UPSERT",
  16. "ResourceRecordSet": {
  17. "Name": "${1}",
  18. "Type": "A",
  19. "TTL": 60,
  20. "ResourceRecords": [
  21. {
  22. "Value": "${2}"
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. _EOF
  30. aws route53 change-resource-record-sets --hosted-zone-id $HOSTEDZONE --change-batch file:///tmp/${1}
  31. rm /tmp/${1}
  32. }
  33. # check that the old ip exists
  34. [ -f $HOME/old_ip ] && export old_ip=$(cat $HOME/old_ip) || export old_ip='X'
  35. # use myip.opendns.com to pull your current IP address
  36. myip=$(dig +short @208.67.222.222 myip.opendns.com)
  37. # update the old ip file
  38. [ -z $myip ] || echo $myip > $HOME/old_ip
  39. # check that the new ip and old ip match, if not, update Route53
  40. [ $myip = $old_ip ] || update $HOSTNAME $myip