Skip to content

Instantly share code, notes, and snippets.

@nolram
Last active September 14, 2022 00:55
Show Gist options
  • Save nolram/468bae3bfc08cbbd1993ab7f0bdd1dd7 to your computer and use it in GitHub Desktop.
Save nolram/468bae3bfc08cbbd1993ab7f0bdd1dd7 to your computer and use it in GitHub Desktop.
Simple shellscript to update Route 53 record with the current public IP
#!/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