Short-circuit before building a query we don't use

This commit is contained in:
Ray Miller 2024-10-23 10:54:11 +01:00
parent 7c24308039
commit c60d6b9891

7
aws/cloudwatch-log-costs.py Executable file → Normal file
View file

@ -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}")