Created
June 9, 2016 20:38
-
-
Save JCotton1123/e0203791aae37f22b27dfce2c7002dbf to your computer and use it in GitHub Desktop.
Export env variables for each AWS CloudFormation stack output
This file contains 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 | |
# This script will find all AWS CloudFormation stacks matching the supplied filter | |
# and export the outputs into environment variables. | |
# | |
# This script is assumed to be run on a host with an IAM profile matching the following: | |
# | |
# { | |
# "Version": "2012-10-17", | |
# "Statement": [ | |
# { | |
# "Sid": "Stmt1465412301000", | |
# "Effect": "Allow", | |
# "Action": [ | |
# "cloudformation:DescribeStacks" | |
# ], | |
# "Resource": [ | |
# "*" | |
# ] | |
# } | |
# ] | |
# } | |
function usage { | |
echo "usage: source fetchenv.sh <env name>" | |
} | |
function main { | |
if [ -z "$1" ]; then | |
usage | |
exit 1 | |
else | |
stack_filter="$1" | |
fi | |
if [ -z "$2" ]; then | |
region=us-east-1 | |
else | |
region="$2" | |
fi | |
stacks=$(aws cloudformation describe-stacks --region "$region" | \ | |
grep "StackName" | grep -i "$stack_filter" | cut -d\" -f4) | |
if [ -z "$stacks" ]; then | |
echo "Unable to locate any CloudFormation stacks matching the supplied name" | |
exit 1 | |
fi | |
for stack in $stacks; do | |
stack_info=$(aws cloudformation describe-stacks --region $region --stack-name $stack --output json) | |
if [[ "$stack_info" =~ "OutputKey" ]]; then | |
read -r -a output_keys <<< $(echo "$stack_info" | jq ".Stacks[].Outputs[].OutputKey") | |
read -r -a output_vals <<< $(echo "$stack_info" | jq ".Stacks[].Outputs[].OutputValue") | |
for ((i=0;i<${#output_keys[@]};++i)); do | |
key=$(echo "${output_keys[i]}" | sed -e 's/^"//' -e 's/"$//') | |
val=$(echo "${output_vals[i]}" | sed -e 's/^"//' -e 's/"$//') | |
echo "export $key=$val" | |
export "$key"="$val" | |
done | |
fi | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Looks like it only cycles over a single Output for a stack though during my CI process, I'll try and figure it out (maybe we need a wait condition).