Skip to content

Instantly share code, notes, and snippets.

@tpschmidt
Created March 1, 2024 06:18
Show Gist options
  • Select an option

  • Save tpschmidt/ff3da01863e304b91ecc08e7e41464da to your computer and use it in GitHub Desktop.

Select an option

Save tpschmidt/ff3da01863e304b91ecc08e7e41464da to your computer and use it in GitHub Desktop.
Set Retention for all AWS CloudWatch Log Groups in all Regions
#!/bin/bash
retention_days=$1
if [ -z "$retention_days" ]; then
echo "Usage: $0 <retention_days>"
exit 1
fi
regions=$(aws ec2 describe-regions \
--query 'Regions[*].RegionName' \
--output text
)
for region in $regions; do
(
echo "Processing region: $region"
log_groups=$(aws logs describe-log-groups \
--region "$region" \
--query 'logGroups[*].logGroupName' \
--output text
)
for log_group in $log_groups; do
echo "[$region] Updating $log_group"
aws logs put-retention-policy \
--region "$region" \
--log-group-name "$log_group" \
--retention-in-days "$retention_days"
done
) &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment