Last active
November 16, 2022 05:33
-
-
Save samermassoud/5906ebfdd44d3af65b54213796f906ea to your computer and use it in GitHub Desktop.
Dynamic DNS - AWS Route 53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @reboot root /path/to/file/update-dns-aws.sh <hosted-zone-id> <dns> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Version": "2012-10-17", | |
| "Statement": [ | |
| { | |
| "Effect": "Allow", | |
| "Action": [ | |
| "route53:GetHostedZone", | |
| "route53:ChangeResourceRecordSets", | |
| "route53:ListResourceRecordSets" | |
| ], | |
| "Resource": "arn:aws:route53:::hostedzone/<target_host_zone_id>" | |
| }, | |
| { | |
| "Effect": "Allow", | |
| "Action": [ | |
| "route53:ListHostedZones" | |
| ], | |
| "Resource": "*" | |
| } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| HOSTED_ZONE_ID=$1 | |
| DOMAIN_NAME=$2 | |
| PUBLIC_RECORD=$3 | |
| if [ -z $PUBLIC_RECORD ] | |
| then | |
| PUBLIC_RECORD=$DOMAIN_NAME. | |
| else | |
| PUBLIC_RECORD=$PUBLIC_RECORD.$DOMAIN_NAME. | |
| fi | |
| IP=$(curl -s https://checkip.amazonaws.com) | |
| echo Found IP=$IP | |
| read -r -d '' R53_ARECORD_JSON << EOM | |
| { | |
| "Changes": [ | |
| { | |
| "Action": "UPSERT", | |
| "ResourceRecordSet": { | |
| "Name": "$PUBLIC_RECORD", | |
| "Type": "A", | |
| "TTL": 300, | |
| "ResourceRecords": [ | |
| { | |
| "Value": "$IP" | |
| } | |
| ] | |
| } | |
| } | |
| ] | |
| } | |
| EOM | |
| echo About to execute change | |
| echo "$R53_ARECORD_JSON" | |
| R53_ARECORD_ID=`aws route53 change-resource-record-sets \ | |
| --hosted-zone-id $HOSTED_ZONE_ID \ | |
| --change-batch "$R53_ARECORD_JSON" \ | |
| --query '[ChangeInfo.Comment, ChangeInfo.Id, ChangeInfo.Status, ChangeInfo.SubmittedAt]' \ | |
| --output text` | |
| #echo $R53_ARECORD_ID | |
| #aws route53 wait resource-record-sets-changed --id $R53_ARECORD_ID | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment