I have a pretty lengthy list of cloudwatch log groups i need to delete....like close to a hundred. Since you have to delete them one at a time I thought a little python script could help me out but now im stuck.
here's my script so far...
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
# )
How do I take the value of 'logGroupName' in i and convert it to a string that the delete_log_group command can use and iterate through to delete all of my log groups I need to be gone? I tried using json.loads and it errored out with the following...
Traceback (most recent call last): File "CWLogCleaner.py", line 18, in file_parsed = json.loads(f) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())
Or am I totally going about this the wrong way?
TIA
Unless you specifically need to save the JSON responses to disk for some other purpose, perhaps you could simply use some variant of this code:
import boto3
def delete_log_streams(prefix=None):
"""Delete CloudWatch Logs log streams with given prefix or all."""
next_token = None
logs = boto3.client('logs')
if prefix:
log_groups = logs.describe_log_groups(logGroupNamePrefix=prefix)
else:
log_groups = logs.describe_log_groups()
for log_group in log_groups['logGroups']:
log_group_name = log_group['logGroupName']
print("Delete log group:", log_group_name)
while True:
if next_token:
log_streams = logs.describe_log_streams(logGroupName=log_group_name,
nextToken=next_token)
else:
log_streams = logs.describe_log_streams(logGroupName=log_group_name)
next_token = log_streams.get('nextToken', None)
for stream in log_streams['logStreams']:
log_stream_name = stream['logStreamName']
print("Delete log stream:", log_stream_name)
logs.delete_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
# delete_log_stream(log_group_name, log_stream_name, logs)
if not next_token or len(log_streams['logStreams']) == 0:
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With