Skip to content

Instantly share code, notes, and snippets.

@cayblood
Last active April 11, 2019 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cayblood/d6598b32f0c6c6ba8d9083825123fcfa to your computer and use it in GitHub Desktop.
Save cayblood/d6598b32f0c6c6ba8d9083825123fcfa to your computer and use it in GitHub Desktop.
User data to update a Route53 subdomain with the new IP address of an EC2 instance every time it boots. Instance must be assigned role with Route53 permissions.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
output : { all : '| tee -a /var/log/cloud-init-output.log' }
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash -xe
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
yum update -y || true
yum install epel-release -y || true
yum install jq -y || true
IP=$( curl http://169.254.169.254/latest/meta-data/public-ipv4 )
JSON=$(jq -n --arg ip "$IP" '{
ChangeBatch: {
Comment: "Update domain with new IP for instance",
Changes: [
{
Action: "UPSERT",
ResourceRecordSet: {
Name: "carl.dev.blockscale.net",
Type: "A",
TTL: 10,
ResourceRecords: [{
Value: $ip
}]
}
}
]
}
}')
HOSTED_ZONE_ID=$( aws route53 list-hosted-zones-by-name | grep -B 1 -e "dev.blockscale.net" | sed 's/.*hostedzone\/\([A-Za-z0-9]*\)\".*/\1/' | head -n 1 )
echo "Hosted zone being modified: $HOSTED_ZONE_ID"
aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$JSON"
--//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment