Skip to content

Instantly share code, notes, and snippets.

@cayblood cayblood/multipart
Last active Apr 11, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.