Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jaredtbates/4c9451d18da330b1b66d4f3704565875 to your computer and use it in GitHub Desktop.
Save jaredtbates/4c9451d18da330b1b66d4f3704565875 to your computer and use it in GitHub Desktop.
A script to use AWS CLI to create or update a CloudFormation stackset
#!/usr/bin/env sh
# Adapted from https://gist.github.com/mdjnewman/b9d722188f4f9c6bb277a37619665e77
usage="Usage: $(basename "$0") stack-set-name template-file administration-role-name execution-role-name
where:
stack-set-name - the stack set name
template-file - the file path of the template
administration-role-name - the name of the administration role
execution-role-name - the name of the execution role
"
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ] || [ "$1" = "usage" ] ; then
printf "$usage"
exit 1
fi
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] ; then
printf "$usage"
exit 1
fi
account_id=$(aws sts get-caller-identity --query Account --output text)
printf "Checking if stack set exists ...\n"
if ! aws cloudformation describe-stack-set --stack-set-name $1 ; then
printf "\nStack set does not exist, creating ...\n"
aws cloudformation create-stack-set \
--stack-set-name $1 \
--template-body file://$2 \
--administration-role-arn arn:aws:iam::$account_id:role/service-role/$3 \
--execution-role-name $4
printf "Stack set create command triggered, please verify it created successfully ...\n"
else
printf "\nStack exists, attempting update ...\n"
set +e
update_output=$( aws cloudformation update-stack-set \
--stack-set-name $1 \
--template-body file://$2 \
--administration-role-arn arn:aws:iam::$account_id:role/service-role/$3 \
--execution-role-name $4 2>&1)
status=$?
set -e
printf "$update_output"
if [ $status -ne 0 ] ; then
# Don't fail for no-op update
if [[ $update_output == *"ValidationError"* && $update_output == *"No updates"* ]] ; then
printf "\nFinished create/update - no updates to be performed\n"
exit 0
else
exit $status
fi
fi
printf "\nStack set update command triggered, please verify it created successfully ...\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment