CloudWatch logs log groups without retention

etc/scripts

·

1 min read

  • outline

When creating AWS resources, log groups are often created in CloudWatch Logs without retention settings, and charged unexpectly. Below is a script that automatically generates a command to check and apply the retention of already created log groups.

  • script
#!/bin/bash
echo "CloudwatchLog without Retention Settings"
for i in $(aws logs describe-log-groups --query logGroups[*].logGroupName[] --output text)
        do
        CloudwatchLogRetention=$(aws logs describe-log-groups --log-group-name-prefix $i --query logGroups[].retentionInDays --output text 2>/dev/null)
        if [ -z "$CloudwatchLogRetention" ];
        then
                echo "aws logs put-retention-policy --log-group-name $i --retention-in-days x"
        else true
        fi
done
echo "================================================================================================================================="