Created
October 9, 2021 01:58
-
-
Save maurostorch/0a03da8770f9c798d964cb67befae935 to your computer and use it in GitHub Desktop.
Remove CloudWatch Rules with targets
This file contains hidden or 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 | |
# Requires AWS CLI v2 | |
#Usage ./remove_events_rules.sh <rule name, or partial name> | |
set -eo pipefail | |
echo "Searching rules for $1"; | |
rules=$(aws events list-rules |jq -r '.Rules[].Name'|grep $1); | |
echo "Deleting rules:\n$rules"; | |
read -p "Confirm (y/n)?" -n 1 -r | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 | |
fi | |
for r in $rules; do | |
echo "\nListing existing targets:"; | |
tids=$(aws events list-targets-by-rule --rule $r | jq -r '.Targets[].Id'); | |
echo "ids=$tids, removing... "; | |
for i in $tids; do | |
aws events remove-targets --rule "$r" --ids "$i"; | |
done | |
echo "OK\nDeleting rule..."; | |
aws events delete-rule --name $r; | |
echo "OK"; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice tool!