Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active February 11, 2024 01:27
Show Gist options
  • Save atheiman/2701e51cc4395d159ad48596c2116576 to your computer and use it in GitHub Desktop.
Save atheiman/2701e51cc4395d159ad48596c2116576 to your computer and use it in GitHub Desktop.
Shell function to wait on CloudFormation StackSet operations in CI/CD pipelines
# Example usage:
# OPERATION_ID="$(aws cloudformation create-stack-instances --stack-set-name MyStackSet \
# --deployment-targets "OrganizationalUnitIds=ou-ab12-abcd1234" \
# --regions us-east-1 \
# --output text)"
# stackSetOperationWait MyStackSet "${OPERATION_ID}"
function stackSetOperationWait {
local cmd="aws cloudformation describe-stack-set-operation --stack-set-name ${1?} --operation-id ${2?}"
# print out the operation being performed
$cmd
# wait for an EndTimestamp attribute (operation has finished)
printf "Waiting for above operation to finish..."
while true; do
sleep 10
local END_TIMESTAMP="$($cmd --query "StackSetOperation.EndTimestamp" --output text)"
if [ "${END_TIMESTAMP}" != "None" ]; then
echo ' Operation finished:'
break
fi
printf '.'
done
# print completed operation
$cmd
local STATUS="$($cmd --query "StackSetOperation.Status" --output text)"
if [ "${STATUS?}" != "SUCCEEDED" ]; then
echo "StackSet operation did not succeed. Stack instances:"
aws cloudformation list-stack-instances --stack-set-name "${1?}"
# If used outside of a CI/CD pipeline this should be a return from the function (not exit)
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment