清除已不存在的lambda的cloudwatch 日志组
1、脚本一
#coding:utf-8 import boto3 lambda_client = boto3.client('lambda',region_name = 'us-west-2') log_client = boto3.client('logs',region_name = 'us-west-2') log_list = log_client.describe_log_groups(logGroupNamePrefix='/aws/lambda/') lambda_list = lambda_client.list_functions() lambdalist = [] for l in lambda_list['Functions']: lambdalist.append(l['FunctionName']) print(lambdalist) for l in log_list['logGroups']:if l['logGroupName'].replace('/aws/lambda/','') not in lambdalist: print(l['logGroupName'].replace('/aws/lambda/','')) response = log_client.delete_log_group(logGroupName=l['logGroupName']) print("删除结果:",response)
2、脚本二
#coding:utf-8 import boto3 lambda_client = boto3.client('lambda',region_name = 'us-west-2') log_client = boto3.client('logs',region_name = 'us-west-2') log_list = log_client.describe_log_groups() lambda_list = lambda_client.list_functions() lambdalist = [] for l in lambda_list['Functions']: lambdalist.append(l['FunctionName']) print(lambdalist) for l in log_list['logGroups']: if l['logGroupName'].startswith('/aws/lambda/'): if l['logGroupName'].replace('/aws/lambda/','') not in lambdalist: print(l['logGroupName'].replace('/aws/lambda/','')) response = log_client.delete_log_group(logGroupName=l['logGroupName']) print("删除结果:",response)
注释: 罗列日志组时一页最多可以显示50条,虽然一次删不干净,多执行两次就OK了。