Last active
September 14, 2022 00:55
-
-
Save nolram/468bae3bfc08cbbd1993ab7f0bdd1dd7 to your computer and use it in GitHub Desktop.
Simple shellscript to update Route 53 record with the current public IP
This file contains 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 | |
# Use EC2 Role to allow the EC2 modify Route 53 record, instead of AWS_SECRET_KEYS | |
AWS_REGION="us-east-1" | |
HOSTED_ZONE_ID="XXXXXXXXXXXXXXXXXXXX" | |
DOMAIN_NAME="example.com" | |
# Get the current IP address | |
EC2_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) | |
JSON_DATA=$(cat <<EOF | |
{ | |
"Comment": "Update IP address", | |
"Changes": [ | |
{ | |
"Action": "UPSERT", | |
"ResourceRecordSet": { | |
"Name": "${DOMAIN_NAME}", | |
"Type": "A", | |
"TTL": 300, | |
"ResourceRecords": [ | |
{ | |
"Value": "${EC2_IP}" | |
} | |
] | |
} | |
} | |
] | |
} | |
EOF | |
) | |
# Update the DNS record | |
RESPONSE=$(/usr/local/bin/aws route53 change-resource-record-sets \ | |
--hosted-zone-id $HOSTED_ZONE_ID \ | |
--change-batch "$JSON_DATA" \ | |
--query "ChangeInfo.Id" \ | |
--output text \ | |
--region $AWS_REGION) | |
echo $RESPONSE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment