小编The*_*eth的帖子

使用 Boto3 批量删除 Cloudwatch 日志组 - delete_log_group

我有一个相当长的 Cloudwatch 日志组列表,需要删除......大概有近一百个。因为你必须一次删除它们,所以我想一个小小的 python 脚本可以帮助我,但现在我卡住了。

到目前为止,这是我的脚本...

import boto3
from botocore.exceptions import ClientError
import json

#Connect to AWS using default AWS credentials in awscli config
cwlogs = boto3.client('logs')

loglist = cwlogs.describe_log_groups(
    logGroupNamePrefix='/aws/lambda/staging-east1-'
)

#writes json output to file...
with open('loglist.json', 'w') as outfile:
    json.dump(loglist, outfile, ensure_ascii=False, indent=4, 
sort_keys=True)

#Opens file and searches through to find given loggroup name
with open("loglist.json") as f:
    file_parsed = json.load(f)

for i in file_parsed['logGroups']:
    print i['logGroupName']


#   cwlogs.delete_log_group(
#       logGroupName='string'   <---here is where im stuck
# …
Run Code Online (Sandbox Code Playgroud)

python json amazon-web-services amazon-cloudwatch boto3

3
推荐指数
1
解决办法
5488
查看次数