Created
October 21, 2023 20:49
-
-
Save suhussai/f891d9142052c5486b75b0d8983a10c2 to your computer and use it in GitHub Desktop.
Bash script to find and delete auto-generated AWS Lambda, CodeBuild and API Gateway log groups using pagination.
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 | |
echo "$(date) cleaning up log groups..." | |
next_token="" | |
while true; do | |
if [[ "${next_token}" == "" ]]; then | |
response=$(aws logs describe-log-groups) | |
else | |
response=$(aws logs describe-log-groups --starting-token "$next_token") | |
fi | |
log_groups=$(echo "$response" | jq -r '.logGroups[].logGroupName | select(. | test("^/aws/lambda/*|^/aws/codebuild/*|^API-Gateway-Execution-Logs_*"))') | |
for i in $log_groups; do | |
echo "$(date) deleting log group with name $i..." | |
aws logs delete-log-group --log-group-name "$i" | |
done | |
next_token=$(echo "$response" | jq '.NextToken') | |
if [[ "${next_token}" == "null" ]]; then | |
# no more results left. Exit loop... | |
break | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment