DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
akloya
akloya

Posted on

AWS CloudWatch Log Group Retention

Amazon CloudWatch Logs is used as centralized place to monitor, store, and access all our log files from different AWS services

CloudWatch organises logs in a log group and when a new log group is created, it’s retention period is set to Never expire by default, which means logs will be retained forever.

Here is a sample python script that helps with changing the retention days to 60.

import boto3
# set the number of retention days 
retention_days = 60
# list the regions you are interested to run this script on
regions=['us-east-1']

for region in regions:
    client = boto3.client('logs',region)
    response = client.describe_log_groups(
    )
    nextToken=response.get('nextToken',None)
    retention = response['logGroups']
    while (nextToken is not None):
        response = client.describe_log_groups(
            nextToken=nextToken
        )
        nextToken = response.get('nextToken', None)
        retention = retention + response['logGroups']
    for group in retention:
        if 'retentionInDays' in group.keys():
            print(group['logGroupName'], group['retentionInDays'],region)
        else:
            print("Retention Not found for ",group['logGroupName'],region)
            setretention = client.put_retention_policy(
                logGroupName=group['logGroupName'],
                retentionInDays=retention_days
                )
            print(setretention)
Enter fullscreen mode Exit fullscreen mode

Once this script is run problem is solved for existing log groups but it would be nice to automate it using cloud watch events to run a python code using lambda in that way all the log groups created going forward will have retention value set.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.