-
-
Save sedeh/08709636e0c889a8b7d289fc5329aec3 to your computer and use it in GitHub Desktop.
Remove CloudWatch Rules with targets
This file contains 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