Been on a new job for a few months now. One of the first things I noticed is we were getting a huge bill for AmazonCloudWatch PutLogEvents and we weren’t quite sure what was the primary culprit. So, ended up creating a small tool to tag all of our log groups and then in Cost Explorer it allowed us to filter by those tags to narrow things down.

Just requires python and the boto3 library:


import sys
import boto3
from botocore.exceptions import ClientError

region = ‘us-east-1’

“””Tag all log groups

Since many log groups are auto created by other functionality, there is no means to add tagging.
This will tag each of our cloudwatch log groups so we can keep track of each from a cost perspective
“””
cwlogs = boto3.client(‘logs’, region)
response = cwlogs.describe_log_groups()
for logGroup in response[‘logGroups’]:
logGroupName = logGroup[‘logGroupName’]
print “Tagging ” + logGroupName + “with Key:log_group, Value: ” + logGroupName.replace(“/”, ““)
print cwlogs.tag_log_group(logGroupName=logGroupName,tags={‘log_group’:logGroupName.replace(“/”,”
“)})