Skip to content

Instantly share code, notes, and snippets.

@mkjsix
Last active January 31, 2020 08:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkjsix/93398280b32aebb7e5379b40e8598b71 to your computer and use it in GitHub Desktop.
Save mkjsix/93398280b32aebb7e5379b40e8598b71 to your computer and use it in GitHub Desktop.
Set retention policies for AWS CloudWatch Log Groups, for a profile and a region
#!/bin/bash
# Example - to set the logs retention policy for your default AWS account in region eu-west-1 to 5 days, execute the command:
# set-retention.sh default eu-west-1 5
export AWS_PROFILE=$1
export AWS_REGION=$2
RETENTION_DAYS=${3:-30}
echo "AWS_PROFILE=$AWS_PROFILE, AWS_REGION=$AWS_REGION, RETENTION_DAYS=$RETENTION_DAYS"
while read -r i;
do (aws logs put-retention-policy --region "$AWS_REGION" --log-group-name "$i" --retention-in-days "$RETENTION_DAYS" );
done < <(aws logs describe-log-groups --region "$AWS_REGION" --query 'logGroups[*].[logGroupName]' | grep / | sed 's/"//g')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment