Skip to content

Instantly share code, notes, and snippets.

@rbenigno
Last active November 13, 2016 17:33
Show Gist options
  • Save rbenigno/c534dd63d09668fb4c7042b4baa94308 to your computer and use it in GitHub Desktop.
Save rbenigno/c534dd63d09668fb4c7042b4baa94308 to your computer and use it in GitHub Desktop.
Update Route53 CNAME for an ec2 instance, from that instance
#!/bin/bash
# Ubuntu: Place this script in /var/lib/cloud/scripts/per-boot
HOST_CNAME=<CNAME to Update>
HOSTED_ZONE_ID=<Route53 Zone ID>
INSTANCE_ID=$(ec2metadata | grep 'instance-id:' | cut -d ' ' -f 2)
PUBLIC_HOSTNAME=$(ec2metadata | grep 'public-hostname:' | cut -d ' ' -f 2)
read -d '' DNS_CHANGE_JSON <<-_EOF_
{
"Comment": "Update host",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "$HOST_CNAME",
"Type": "CNAME",
"TTL": 60,
"ResourceRecords": [
{
"Value": "$PUBLIC_HOSTNAME"
}
]
}
}
]
}
_EOF_
aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --change-batch "$DNS_CHANGE_JSON"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment