forked from antonbabenko/terraform-cost-estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.jq
524 lines (504 loc) · 17.1 KB
/
terraform.jq
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# This jq-file extracts cost keys from given Terraform file (plan as json and any version of Terraform state).
#
# jq version 1.6 or newer is required.
#
# Repository: https://github.com/antonbabenko/terraform-cost-estimation
# License: MIT
def debug($msg): $msg | debug | empty;
def outline($msg): "---> \($msg)";
def isnull: . == null;
def isstr: type == "string";
def isarray: type == "array";
def isobject: type == "object";
def isiterable: type|. == "array" or . == "object";
def isblankstr: type == "string" and . == "";
def isblank: isiterable and (.|length) == 0;
def isidle: isnull or isblankstr or isblank;
def idle_paths: [path(..|select(isidle))];
def del_nulls: delpaths([paths(isnull)]);
def del_blanks: delpaths([paths(isblank)]);
def del_idles: delpaths(idle_paths);
def del_idles_recursive:
walk(if isidle then . elif isiterable then delpaths([paths(isidle)]) else . end);
def getpaths(PATHS): . as $in | reduce PATHS[] as $p ({}; .[$p[0]] = ($in | getpath($p)));
def uniq: reduce .[] as $x ([]; if . | contains([$x]) | not then . + [$x] else . end);
def insideof($g): . as $i| $g | map(.==$i) | any;
def mul: reduce .[] as $x(1; .*$x);
def array_mul_tail: [.[0],(.[1:]|mul)];
def array_group_sum:
group_by(.[0]) as $x|[$x[]|reduce .[] as $x(["", 0]; [$x[0], .[1]+$x[1]])]
;
def pack_cost_keys: map(join("|"));
def compact_cost_keys:
sort_by(.[0]) | array_group_sum | pack_cost_keys
;
def vars:
{
platform: "linux",
ec2_prefix: "ec2",
fallback_region: ($ENV.AWS_DEFAULT_REGION // $ENV.AWS_REGION // "eu-west-1")
}
;
def group_resources:
group_by(.)[]|[.[0], (.|length)] | join("|")
;
def group_and_sum:
reduce .[] as $x (
{}; if .[$x[0]] == null
then .[$x[0]]=$x[1]
else .[$x[0]]+=$x[1] end)
;
def parse_arn:
split(":")[3] # Take region
;
def get_cost_keys_from_ec2_instances:
vars as $v
| {
region: (.arn | parse_arn? // $v.fallback_region),
tenancy: (if .tenancy != "dedicated" then "shared" else . end),
instance_type,
capacity: (.capacity // 1),
}
;
def construct_ec2_cost_key:
vars as $v
|[ ([
$v.ec2_prefix
, .region
, .instance_type
, .tenancy
, $v.platform
]
| join("#")
| ascii_downcase)
, .capacity
] #| join("|")
;
def extract_ec2_cost_info:
get_cost_keys_from_ec2_instances
| construct_ec2_cost_key
;
def get_cost_keys_from_gpu_instances:
vars as $v
|{
region: (.arn | parse_arn? // $v.fallback_region),
gpu_type: .elastic_gpu_specifications[].type,
capacity: (.capacity // 1),
instance_type,
id,
}
;
def construct_gpu_cost_key:
vars as $v
|[ ([
$v.ec2_prefix
, .region
, .gpu_type
]
| join("#")
| ascii_downcase)
, .capacity
] #| join("|")
;
def extract_gpu_cost_info:
get_cost_keys_from_gpu_instances
| construct_gpu_cost_key
;
def aws:
################################################################# Guards
def exists(pth):
if pth != null then
pth
else
. as $i |
path(pth) as $p |
$p | join(".") as $d |
$i | getpath($p[:-1])|keys|join(", ") as $t |
error("Path \".\($d)\" doesn't found in a json file.\n"
+ "Same level keys: \($t)")
end
;
################################################################# Adapters
def tf_lt_12_adapter:
[ .modules[].resources
|to_entries[]
|{ name: .key,
type: .value.type,
instances:[.value.primary + {dependencies:.value.depends_on}]
}]
;
################################################################# Data shapers
def clear_io(expr):
del_idles_recursive | [expr | del_idles_recursive]
;
def shape_attributes:
{
id, arn, instance_type,
# Autoscaling groups specific
min_size, spot_price, desired_capacity,
# LB specific
load_balancer_type,
# Launch templates and configuration specific
launch_configuration,
launch_template: [.launch_template[]? // {} | {id}],
launch_template_config:
[.launch_template_config[]? // {} | {
launch_template_specification:
[.launch_template_specification[]? // {} | {
launch_template_id
}]
}],
# GPU specific
elastic_gpu_specifications:
[.elastic_gpu_specifications[]? // {} | {type}],
# EC2 fleet specific
target_capacity_specification:
[.target_capacity_specification[]? // {} | {
default_target_capacity_type,
total_target_capacity,
}],
# EBS specific
device_name,
volume_size,
volume_type,
iops,
type,
size,
source_region,
}
;
def shape_resources(root):
root |
{ #"module", mode, each, name,
type, provider,
instances: [.instances[]? // {} | {
attributes: .attributes | shape_attributes,
#dependencies,
}]
}
;
def shape_resource_changes(root):
root |
{ "module", mode, each, name, address,
type, provider_name,
change: .change | {
before: .before | shape_attributes,
after: .after | shape_attributes
},
}
;
################################################################# Data extractors
def extract_resources:
if .modules then clear_io(shape_resources(tf_lt_12_adapter[]))
elif .resources then clear_io(shape_resources(.resources[]))
else error("Can't read resources")
end
;
def extract_resource_changes:
if .resource_changes then clear_io(shape_resource_changes(.resource_changes[]))
else error("Can't read resource_changes")
end
;
def aws_ebs_block_devices(attr; inst):
def extract(cond; cost_type):
vars as $v |
map(select(cond))[]
| . as $r
| inst
| if (type| . == "object") then [.] else . end # hack for plans
|. [] | attr
| { region: (.arn | parse_arn? // $v.fallback_region),
cost_type: cost_type,
type: $r.type,
volume_size: (.volume_size // 0),
size: (.size // 0),
iops: (.iops // 0),
source_region: (.source_region // $v.fallback_region),
}
;
[(
extract(.type == "aws_ebs_snapshot"; "snapshot"),
extract(.type == "aws_ebs_snapshot_copy"; "snapshot"),
extract(.type == "aws_ebs_volume";
if .type == "standard" then "standard"
elif .type == "io1" then "io1"
else "gp2" end)
) | (
if .type == "aws_ebs_snapshot" then
["ec2#\(.region)#\(.cost_type)", .volume_size]
elif .type == "aws_ebs_snapshot_copy" then
["ec2#\(.source_region)#\(.cost_type)", .volume_size]
elif .type == "aws_ebs_volume" then (
if .cost_type == "io1" then
["ec2#\(.region)#\(.cost_type)", .size],
["ec2#\(.region)#piops", .iops]
else
["ec2#\(.region)#\(.cost_type)", .size]
end
)
else ["ec2#\(.region)#\(.cost_type)", .size]
end
)
]
;
def aws_ec2_fleet(attr; inst):
[ map(select(.type == "aws_ec2_fleet" and inst))[] // {}
| (inst // {})
| map(attr)[] // {}
| select( # Common filtration on instances level
.target_capacity_specification[]?
| .default_target_capacity_type == "on-demand"
and .total_target_capacity > 0
)
| [ .target_capacity_specification[]? # Gathering all capacities
| select(.default_target_capacity_type == "on-demand"
and .total_target_capacity > 0)
| .total_target_capacity
] as $capacities
| .launch_template_config[]? // {}
| .launch_template_specification[]? // {}
| {launch_template_id, capacity: $capacities | add}
#| {(.launch_template_id): .capacity}
| [.launch_template_id, .capacity]
] | group_and_sum
;
def aws_instance(attr; inst):
[ map(select(.type == "aws_instance" and inst))[] // empty
| inst // {}
| if (type| . == "object") then [.] else . end # hack for plans
] | add // []
| map(attr)
;
def aws_autoscaling_group(inst):
#select(contains({type: "aws_autoscaling_group"}))
map(select(.type == "aws_autoscaling_group" and inst))
;
def ag_filter(attr):
# Filtration of autoscaling groups that doesn't fit.
map(select(
attr.desired_capacity>=1
or attr.min_size>=1
or attr.spot_price
))
;
def ag_instances(attr; inst):
[ .[]
| inst
| if (type| . == "object") then [.] else . end # hack for plans
] | (add // [])
;
def ag_instances_with_lt(attr):
map(select(attr.launch_template))
;
def ag_instances_with_lc(attr):
map(select(attr.launch_configuration))
;
def lt_ids(attr):
[.[] | attr.launch_template[].id] | uniq
;
def lc_ids(attr):
[.[] | attr.launch_configuration] | uniq
;
def desired_capacities(attr; inst):
[ .[] | inst
| if (type| . == "object") then [.] else . end # hack for plans
| .[] | attr as $a | $a
| [.launch_configuration? // (.launch_template? // [] | .[].id)] #as $ids
| .[] | [., ($a.desired_capacity | if . == 0 then $a.min_size // 0 else . end)]
] | group_and_sum # => {launch_..._id: total_desired_capacity}
;
def aws_launch_template(inst):
map(select(.type == "aws_launch_template" and inst))
;
def aws_launch_configuration(inst):
map(select(.type == "aws_launch_configuration" and inst))
;
def launch_instances($ids; attr; inst):
[[.[] | inst
| if (type | . == "object") then [.] else . end # hack for plans
] | (add // [])[] | select(attr.id|insideof($ids))]
| map(attr) # Reduce depth
;
def bind_capacities($caps):
[.[] | . + {capacity: $caps[.id]}]
;
################################################################# Compute costs funcs
def cost_instances:
map(extract_ec2_cost_info)
;
def cost_gpus:
map(extract_gpu_cost_info)
;
def aws_loadbalancers(attr; inst):
def extract(cond; _type):
vars as $v |
map(select(cond))[]
| inst
| if (type | . == "object") then [.] else . end # hack for plans
| .[] | attr
| { region: (.arn | parse_arn? // $v.fallback_region),
type: _type}
;
[(
extract(.type == "aws_lb" or .type == "aws_alb";
if .load_balancer_type == "network" then "nlb" else "alb" end),
extract(.type == "aws_elb"; "elb"),
extract(.type == "aws_nat_gateway"; "nat")
) | ["ec2#\(.region)#\(.type)", 1]]
#| group_resources
;
################################################################# Process data
def process_resources(attr; inst):
. as $r |
aws_instance(attr; inst) as $ai |
aws_autoscaling_group(inst) as $ag |
($ag | ag_instances(attr; inst)
| ag_filter(attr)) as $agi |
($agi | ag_instances_with_lt(attr)) as $ilt |
($agi | ag_instances_with_lc(attr)) as $ilc |
($ilt | lt_ids(attr)) as $lti |
($ilc | lc_ids(attr)) as $lci |
($ag | desired_capacities(attr; inst)) as $dc |
(aws_launch_template(inst)) as $lt |
($lt | launch_instances($lti; attr; inst)
| bind_capacities($dc)) as $lt_ins |
(aws_launch_configuration(inst)) as $lc |
($lc | launch_instances($lci; attr; inst)
| bind_capacities($dc)) as $lc_ins |
aws_ec2_fleet(attr; inst) as $af |
($lt | launch_instances(($af|keys); attr; inst)
| bind_capacities($af)) as $af_ins |
################################################################ Compute costs
($ai | cost_instances) as $cai |
($lt_ins | cost_instances) as $clti |
($lt_ins | cost_gpus) as $cgti |
($lc_ins | cost_instances) as $clci |
($lc_ins | cost_gpus) as $cgci |
($af_ins | cost_instances) as $cafi |
aws_loadbalancers(attr; inst) as $lb |
aws_ebs_block_devices(attr; inst) as $ebs |
($cai + $clti + $cgti + $clci + $cgci + $lb + $cafi + $ebs) as $ck
############################################################### Result object
| {
resources: $r,
instances: $ai,
autoscaling_groups: {
groups: $ag,
instances: {
all: $agi,
lt: {
instances: $ilt,
ids: $lti,
},
lc: {
instances: $ilc,
ids: $lci,
},
},
capacities: $dc,
},
ec2_fleets: {
capacities: $af,
instances: $af_ins,
},
launch_template: {
instances: $lt_ins,
},
launch_configuration: {
instances: $lc_ins,
},
cost: {
loadbalancers: $lb | pack_cost_keys,
instances: $cai | pack_cost_keys,
ec2_fleet: {
instances: $cafi | pack_cost_keys,
},
launch_template: {
instances: $clti | pack_cost_keys,
gpu_instances: $cgti | pack_cost_keys,
},
launch_configuration: {
instances: $clci | pack_cost_keys,
gpu_instances: $cgci | pack_cost_keys,
},
block_devices: $ebs | pack_cost_keys,
keys: $ck | compact_cost_keys,
},
}
;
def process_resource_changes:
. as $r | $r
| process_resources(.change.before; .) as $before
| process_resources(.change.after; .) as $after
| {
#before: $before | del_idles_recursive,
#after: $after | del_idles_recursive,
cost: {
loadbalancers: {
before: $before.cost.loadbalancers,
after: $after.cost.loadbalancers,
},
instances: {
before: $before.cost.instances,
after: $after.cost.instances,
},
ec2_fleet: {
before: $before.cost.ec2_fleet,
after: $after.cost.ec2_fleet,
},
launch_template: {
before: $before.cost.launch_template,
after: $after.cost.launch_template,
},
launch_configuration: {
before: $before.cost.launch_configuration,
after: $after.cost.launch_configuration,
},
block_devices: {
before: $before.cost.block_devices,
after: $after.cost.block_devices,
},
keys: {
before: $before.cost.keys,
after: $after.cost.keys,
}
}
}
;
def process:
if .modules or .resources then
extract_resources | process_resources(.attributes; .instances)
elif .resource_changes then
. | extract_resource_changes | process_resource_changes
else error("Unknown json file structure")
end
;
process
;
def AWS:
############################################################### Shortcuts object
aws |
{
r: .resources,
i: .instances,
f: .ec2_fleets,
lt: .launch_template,
lc: .launch_configuration,
ag: .autoscaling_groups,
c: .cost,
}
;
def parse:
{
version: "0.2.0",
keys: aws.cost.keys,
}
;
empty
, parse
, if $ARGS.named.extra != null then
outline("Extra info")
, aws
else empty
end
# vim:ts=4:sw=4:et