-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecs_cost_analyzer.py
More file actions
177 lines (141 loc) · 6.38 KB
/
ecs_cost_analyzer.py
File metadata and controls
177 lines (141 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import argparse
import boto3
from datetime import datetime, timedelta
from tabulate import tabulate
from collections import defaultdict
def get_cost_and_usage(days, cluster_name=None):
client = boto3.client('ce')
end_date = datetime.now()
start_date = end_date - timedelta(days=days)
# Format dates as YYYY-MM-DD
start_str = start_date.strftime('%Y-%m-%d')
end_str = end_date.strftime('%Y-%m-%d')
print(f"Fetching cost data from {start_str} to {end_str}...")
# Define the filter
# We want to capture costs related to ECS.
# A good starting point is to filter by the existence of the ECS Service Name tag.
# If a cluster name is provided, we add that to the filter.
filter_args = {
'Tags': {
'Key': 'aws:ecs:serviceName',
'Values': [] # Empty list means "tag exists" (match anything) - wait, CE API requires specific values or "Key" existence check is different.
# Actually, 'Tags' filter usually requires Key and Values.
# To match "Tag exists", we might need a different approach or just filter by Service 'Amazon Elastic Container Service' + others.
# However, the most accurate way for "ECS Service Cost" is the tag.
# Let's try to filter by the Service dimension first to be safe, then group by tags.
}
}
# Better approach: Filter by Dimensions -> Service: ECS, EC2, Data Transfer
# AND (Optional) Tag: aws:ecs:clusterName = <cluster_name>
# We define the base filter for services we care about
base_filter = {'Dimensions': {'Key': 'SERVICE', 'Values': ['Amazon Elastic Container Service', 'Amazon Elastic Compute Cloud - Compute', 'AWS Data Transfer']}}
search_filter = base_filter
if cluster_name:
# If cluster is specified, we MUST filter by it.
# Note: This assumes 'aws:ecs:clusterName' tag is active and populated.
search_filter = {
'And': [
search_filter,
{'Tags': {'Key': 'aws:ecs:clusterName', 'Values': [cluster_name]}}
]
}
# We want to group by Service Name (Tag) and Usage Type.
# Cost Explorer allows max 2 grouping dimensions.
group_by = [
{'Type': 'TAG', 'Key': 'aws:ecs:serviceName'},
{'Type': 'DIMENSION', 'Key': 'USAGE_TYPE'}
]
try:
response = client.get_cost_and_usage(
TimePeriod={'Start': start_str, 'End': end_str},
Granularity='DAILY',
Filter=search_filter,
Metrics=['UnblendedCost'],
GroupBy=group_by
)
except Exception as e:
print(f"Error fetching data: {e}")
return []
return response.get('ResultsByTime', [])
def process_results(results):
# List to hold rows: [Date, Service Name, Usage Type, Cost]
processed_rows = []
for day_result in results:
date = day_result['TimePeriod']['Start']
groups = day_result.get('Groups', [])
for group in groups:
keys = group['Keys']
# Keys are [TagValue, UsageType] because of our GroupBy order
service_tag = keys[0]
usage_type = keys[1]
# The tag value might be 'aws:ecs:serviceName$' (empty) if the tag is missing
if service_tag.startswith('aws:ecs:serviceName$'):
service_name = service_tag.split('$')[1]
if not service_name:
service_name = "No Service Tag"
else:
service_name = service_tag
amount = float(group['Metrics']['UnblendedCost']['Amount'])
# Filter out effectively zero costs
if amount > 0.0001:
processed_rows.append([date, service_name, usage_type, amount])
return processed_rows
def main():
parser = argparse.ArgumentParser(description='ECS Cost Analyzer')
parser.add_argument('--days', type=int, default=7, help='Number of days to look back (default: 7)')
parser.add_argument('--cluster', type=str, help='Filter by ECS Cluster Name (requires aws:ecs:clusterName tag)')
args = parser.parse_args()
results = get_cost_and_usage(args.days, args.cluster)
if not results:
print("No data found or error occurred.")
return
table_data = process_results(results)
# Sort by Date (descending), then Service Name, then Cost (descending)
table_data.sort(key=lambda x: (x[0], x[1], x[3]), reverse=True)
# Format cost for display and add daily totals
formatted_table = []
total_cost = 0.0
# Group by Date to calculate daily totals
current_date = None
daily_total = 0.0
daily_rows = []
# Helper to flush daily rows
def flush_day(date, rows, total):
if not rows:
return []
# Add rows for the day
flushed = []
for r in rows:
flushed.append([r[0], r[1], r[2], f"${r[3]:.4f}"])
# Add daily total
flushed.append([date, "DAILY TOTAL", "", f"${total:.4f}"])
# Add separator row (empty) if needed, but tabulate handles grid well.
# Maybe just a visual separator in the list isn't needed with 'grid' fmt,
# but the Total row itself acts as a separator.
return flushed
for row in table_data:
# row is [Date, Service, UsageType, Cost]
date = row[0]
cost = row[3]
if current_date is not None and date != current_date:
# New day, flush previous day
formatted_table.extend(flush_day(current_date, daily_rows, daily_total))
daily_rows = []
daily_total = 0.0
current_date = date
daily_rows.append(row)
daily_total += cost
total_cost += cost
# Flush last day
if current_date:
formatted_table.extend(flush_day(current_date, daily_rows, daily_total))
print(f"\nDaily Cost Report for last {args.days} days")
if args.cluster:
print(f"Cluster: {args.cluster}")
print("-" * 80)
print(tabulate(formatted_table, headers=['Date', 'Service Name', 'Usage Type', 'Cost'], tablefmt='grid'))
print(f"\nTotal Cost (All Days): ${total_cost:.4f}")
print("-" * 80)
print("Note: 'No Service Tag' means costs were found but the 'aws:ecs:serviceName' tag was missing.")
if __name__ == '__main__':
main()