CloudWatch Log Groups deletion using AWS Lambda function

0

Hi Aws, Need a lambda function to delete log group with no retention and if the retention period <365 days, and with no KMS encryption configured

Aman
已提問 1 年前檢視次數 329 次
3 個答案
1
profile picture
專家
已回答 1 年前
profile picture
專家
已審閱 1 年前
0

I have edited the code created in this question to the code that meets your requirements.
https://repost.aws/questions/QUMcZ1aqV1Rwu-IozegdMF6g/delete-cloudwatch-log-groups-using-aws-lambda-function

The following code inserts an if statement in line 10 to determine if the retention period is less than 365 days and if the file is not encrypted by KMS.

import boto3

def lambda_handler(event, context):
    logs_client = boto3.client('logs')
    response = logs_client.describe_log_groups()

    for log_group in response['logGroups']:
        if 'retentionInDays' in log_group:
            retentionin_days = log_group['retentionInDays']
            if retentionin_days < 365 and 'kmsKeyId' not in log_group:
                log_group_name = log_group['logGroupName']
                logs_client.delete_log_group(logGroupName=log_group_name)
profile picture
專家
已回答 1 年前
  • what if the log groups have never expire? how to delete that?

0

what if the log groups have never expire? how to delete that?

Aman
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南