Skip to content

Instantly share code, notes, and snippets.

@wokoman
Last active March 12, 2024 14:22
Show Gist options
  • Save wokoman/f036637a1ad57caeeb4d05eba24dbf8b to your computer and use it in GitHub Desktop.
Save wokoman/f036637a1ad57caeeb4d05eba24dbf8b to your computer and use it in GitHub Desktop.
Script to cleanup failed CloudFormation change sets
#!/bin/bash
# Takes args in this order: region, prefix, profile
# Example: bash change-sets-cleanup.sh eu-west-1 kbc- AWS-profile-foo-bar-eu-west-1-AWSAdministratorAccess
# Setting AWS_PROFILE variable to be super safe
export AWS_PROFILE=$3
stacks=$(aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE --query "StackSummaries[?starts_with(StackName, \`$2\`) == \`true\`].StackName" --output text --region "$1" --profile "$3")
for stack in $stacks; do
echo "${stack}: cleaning up change sets"
changesets=$(aws cloudformation list-change-sets --stack-name "${stack}" --query 'Summaries[?Status==`FAILED`].ChangeSetId' --output text --region "$1" --profile "$3")
for changeset in $changesets; do
echo "${stack}: deleting change set ${changeset}"
aws cloudformation delete-change-set --change-set-name "${changeset}" --region "$1" --profile "$3"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment