Last active
April 11, 2019 04:58
-
-
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.
This file contains hidden or 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
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