Created
April 4, 2017 14:32
-
-
Save jillesvangurp/4f3dcbf07f9298d0d548a656f0051884 to your computer and use it in GitHub Desktop.
quick and dirty dns registration of cf stack with route53
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
#! /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