diff --git a/aws/cloudwatch-log-costs.py b/aws/cloudwatch-log-costs.py index 9c23576..aa6e3f1 100755 --- a/aws/cloudwatch-log-costs.py +++ b/aws/cloudwatch-log-costs.py @@ -29,16 +29,16 @@ for page in paginator.paginate(Namespace="AWS/Logs", MetricName="IncomingBytes") "Metric": { "Namespace": metric["Namespace"], "MetricName": metric["MetricName"], - "Dimensions": metric["Dimensions"] + "Dimensions": metric["Dimensions"], }, "Period": int(period.total_seconds()), - "Stat": "Sum" + "Stat": "Sum", }, } if not metric["Dimensions"]: continue query_names[f"q{n}"] = metric["Dimensions"][0]["Value"] - n = n+1 + n = n + 1 queries.append(query) @@ -47,10 +47,17 @@ for page in paginator.paginate(Namespace="AWS/Logs", MetricName="IncomingBytes") # otherwise we would have to chunk up the calls to GetMetricData. results = [] data_paginator = cw.get_paginator("get_metric_data") -for data_page in data_paginator.paginate(MetricDataQueries=queries, StartTime=start_time, EndTime=end_time): +for data_page in data_paginator.paginate( + MetricDataQueries=queries, StartTime=start_time, EndTime=end_time +): for result in data_page["MetricDataResults"]: if result["Values"]: - results.append({"LogGroup": query_names[result["Id"]], "IncomingBytes": result["Values"][0]}) + results.append( + { + "LogGroup": query_names[result["Id"]], + "IncomingBytes": result["Values"][0], + } + ) results.sort(key=lambda x: x["IncomingBytes"], reverse=True) @@ -62,6 +69,6 @@ for result in results: # CloudWatch logs: 5GiB free ingestion then $0.50 / GiB # Estimate a monthly cost by doubling the incoming_bytes counted over the last 14 days -incoming_gb = 2*incoming_bytes_total/(1024*1024*1024) +incoming_gb = 2 * incoming_bytes_total / (1024 * 1024 * 1024) cost = (incoming_gb - 5.0) * 0.5 print(f"Estimated monthly cost: ${cost:.2f}")