Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hermes-pimentel/75e7369a14b1a219676716c33ed0849f to your computer and use it in GitHub Desktop.
Save hermes-pimentel/75e7369a14b1a219676716c33ed0849f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
'''
This script sets a retention on all the logGroups that don't have one set
'''
import boto3
import logging
# Retention it will be set to
RETENTION = 30
logging.basicConfig(format='%(message)s', level=logging.INFO)
client = boto3.client('logs')
paginator = client.get_paginator('describe_log_groups')
for page in paginator.paginate(limit = 50):
for group in page.get('logGroups', []):
_ret = group.get('retentionInDays', -1)
name = group.get('logGroupName', '')
if _ret == -1 and len(name) > 0:
logging.info('Setting retention to %s days for %s', RETENTION, name)
client.put_retention_policy(logGroupName=name, retentionInDays=RETENTION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment