Skip to content

Instantly share code, notes, and snippets.

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 jillesvangurp/4f3dcbf07f9298d0d548a656f0051884 to your computer and use it in GitHub Desktop.
Save jillesvangurp/4f3dcbf07f9298d0d548a656f0051884 to your computer and use it in GitHub Desktop.
quick and dirty dns registration of cf stack with route53
#! /bin/bash
# call ./registerPrivateIpForCloudFormationStack.sh domainName stackName hostedZoneName
# requires aws-cli and jq installed, you may want to
function hostedzoneId() {
export hostedzone
hostedzone=$(aws route53 list-hosted-zones | jq --raw-output '.HostedZones[] | select(.Name == "$1").Id')
echo "$hostedzone"
}
function instanceIpsForStack() {
export ips
ips=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:aws:cloudformation:stack-name,Values=$1" | jq --raw-output '.Reservations[].Instances[0].PrivateIpAddress')
echo "$ips"
}
export json
json='
{
"HostedZoneId":"",
"ChangeBatch": {
"Comment": "Update record to reflect new IP address for a system ",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "tobereplacedwithparam",
"Type": "A",
"TTL": 10,
"ResourceRecords": [
]
}
}
]
}
}'
json=$(echo "$json" | jq ".HostedZoneId = \"$(inbotiohostedzone $3)\"")
json=$(echo "$json" | jq ".ChangeBatch.Changes[0].ResourceRecordSet.Name = \"$1\"")
for ip in $(instanceIpsForStack $2); do
json=$(echo "$json" | jq ".ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords |= . + [{\"Value\":\"$ip\"}]")
done
#echo "$(echo $json | jq '')"
aws route53 change-resource-record-sets --cli-input-json "$json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment