Skip to content

Instantly share code, notes, and snippets.

@mdjnewman
Last active January 4, 2024 13:44
Show Gist options
  • Save mdjnewman/b9d722188f4f9c6bb277a37619665e77 to your computer and use it in GitHub Desktop.
Save mdjnewman/b9d722188f4f9c6bb277a37619665e77 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
usage="Usage: $(basename "$0") region stack-name [aws-cli-opts]
where:
region - the AWS region
stack-name - the stack name
aws-cli-opts - extra options passed directly to create-stack/update-stack
"
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "help" ] || [ "$1" == "usage" ] ; then
echo "$usage"
exit -1
fi
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then
echo "$usage"
exit -1
fi
shopt -s failglob
set -eu -o pipefail
echo "Checking if stack exists ..."
if ! aws cloudformation describe-stacks --region $1 --stack-name $2 ; then
echo -e "\nStack does not exist, creating ..."
aws cloudformation create-stack \
--region $1 \
--stack-name $2 \
${@:3}
echo "Waiting for stack to be created ..."
aws cloudformation wait stack-create-complete \
--region $1 \
--stack-name $2 \
else
echo -e "\nStack exists, attempting update ..."
set +e
update_output=$( aws cloudformation update-stack \
--region $1 \
--stack-name $2 \
${@:3} 2>&1)
status=$?
set -e
echo "$update_output"
if [ $status -ne 0 ] ; then
# Don't fail for no-op update
if [[ $update_output == *"ValidationError"* && $update_output == *"No updates"* ]] ; then
echo -e "\nFinished create/update - no updates to be performed"
exit 0
else
exit $status
fi
fi
echo "Waiting for stack update to complete ..."
aws cloudformation wait stack-update-complete \
--region $1 \
--stack-name $2 \
fi
echo "Finished create/update successfully!"
@viveksamaga
Copy link

Am getting the below error while trying to update the existing stack. What do I need to change in the script?
An error occurred (AlreadyExistsException) when calling the CreateStack operation: Stack [Test-stack] already exists

@mdjnewman
Copy link
Author

@viveksamaga I've not tested this since 2016 sorry, it's possible the AWS CLI has introduced some breaking changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment