From c60d6b9891d91f91e9b281b1f470652126536b42 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Wed, 23 Oct 2024 10:54:11 +0100 Subject: [PATCH] Short-circuit before building a query we don't use --- aws/cloudwatch-log-costs.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) mode change 100755 => 100644 aws/cloudwatch-log-costs.py diff --git a/aws/cloudwatch-log-costs.py b/aws/cloudwatch-log-costs.py old mode 100755 new mode 100644 index aa6e3f1..f46ab92 --- a/aws/cloudwatch-log-costs.py +++ b/aws/cloudwatch-log-costs.py @@ -23,6 +23,8 @@ queries = [] paginator = cw.get_paginator("list_metrics") for page in paginator.paginate(Namespace="AWS/Logs", MetricName="IncomingBytes"): for metric in page["Metrics"]: + if not metric["Dimensions"]: + continue query = { "Id": f"q{n}", "MetricStat": { @@ -35,8 +37,6 @@ for page in paginator.paginate(Namespace="AWS/Logs", MetricName="IncomingBytes") "Stat": "Sum", }, } - if not metric["Dimensions"]: - continue query_names[f"q{n}"] = metric["Dimensions"][0]["Value"] n = n + 1 queries.append(query) @@ -62,13 +62,12 @@ for data_page in data_paginator.paginate( results.sort(key=lambda x: x["IncomingBytes"], reverse=True) incoming_bytes_total = 0 - for result in results: incoming_bytes_total += result["IncomingBytes"] print("{LogGroup}: {IncomingBytes}".format(**result)) -# 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) +# CloudWatch logs: 5GiB free ingestion then $0.50 / GiB cost = (incoming_gb - 5.0) * 0.5 print(f"Estimated monthly cost: ${cost:.2f}")