Last active
May 29, 2023 12:23
-
-
Save sagarchauhan005/d05afd6d2c7c1a8c21eca6c2be1337fd to your computer and use it in GitHub Desktop.
How to reduce Lambda log retention duration with a simple script for CloudWatch logs and save cost?
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 | |
# Get all log groups created by AWS Lambda | |
log_groups=$(aws logs describe-log-groups --log-group-name-prefix /aws/ --query 'logGroups[].logGroupName' --output text) | |
# Loop through each log group | |
for log_group in $log_groups; do | |
# Update the retention period for the log group to 3 days | |
aws logs put-retention-policy --log-group-name "$log_group" --retention-in-days 3 | |
# Delete all logs older than 3 days | |
logs=$(aws logs describe-log-streams --log-group-name "$log_group" --query 'logStreams[?creationTime<=`'$(date -d '3 days ago' +%s)000'`].logStreamName' --output text) | |
for log in $logs; do | |
response="" | |
while [[ "$response" != "" ]]; do | |
response=$(aws logs delete-log-stream --log-group-name "$log_group" --log-stream-name "$log" 2>&1) | |
if echo "$response" | grep -q "ThrottlingException"; then | |
sleep 1 | |
fi | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment