Skip to content

Instantly share code, notes, and snippets.

@giehlman
Last active March 12, 2020 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giehlman/e5106eb6ba86387a48570cc3a52f916b to your computer and use it in GitHub Desktop.
Save giehlman/e5106eb6ba86387a48570cc3a52f916b to your computer and use it in GitHub Desktop.
Removes cloudwatch loggroups that contain a given search string using aws-cli
#!/usr/bin/env bash
#title : remove-loggroups.sh
#description : Removes cloudwatch loggroups that contain a given search string using aws-cli
#author : Christian-André Giehl <christian@emailbrief.de>
#date : 20200312
#version : 1.0
#usage : sh remove-loggroups.sh <search-string>
#==============================================================================
# Exits in case the supplied state is != 0. State is typically supplied via $?
exitOnError() {
state=$1
msg=$2
if [ $state -ne 0 ]; then
echo "!!! ${msg}"
echo "### Exiting."
exit $state
fi
}
confirmPrompt() {
if [ ! -z "${HEADLESS}" ]; then
echo "Headless! Auto-confirmed!"
return 0
fi
read -p "-----> DO YOU WANT TO ${1}? [Yy]es " -n 1 -r </dev/tty
if [[ $REPLY =~ ^[Yy]$ ]]
then
return 0
fi
return 1
}
printUsage() {
echo "!!! Please specify exactly 1 arg! !!!"
echo " Usage: sh remove-loggroups.sh <search-string>"
echo " with:"
echo " <search-string> a string that all your search groups are grepped for"
}
if [ $# -ne 1 ]; then
printUsage
exit 1
fi
SEARCH_STRING=$1
REGION=eu-central-1
for loggroup in $(aws logs describe-log-groups --output table --region ${REGION}|awk '{print $6}'|grep ${SEARCH_STRING})
do
echo "Removing log group '${loggroup}'..."
confirmPrompt "remove loggroup '${loggroup}'"
exitOnError $? "!!! ABORTED !!!"
aws logs delete-log-group --log-group-name ${loggroup}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment