-
Notifications
You must be signed in to change notification settings - Fork 32
/
aws_superseded_instances.pt
1332 lines (1155 loc) · 43.6 KB
/
aws_superseded_instances.pt
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name "AWS Superseded EC2 Instances"
rs_pt_ver 20180301
type "policy"
short_description "Checks for AWS EC2 instance types that have been superseded and, optionally, updates the instance type. See the [README](https://github.com/flexera-public/policy_templates/tree/master/cost/aws/superseded_instances) and [docs.flexera.com/flexera/EN/Automation](https://docs.flexera.com/flexera/EN/Automation/AutomationGS.htm) to learn more."
long_description ""
severity "low"
category "Cost"
default_frequency "weekly"
info(
version: "2.3.2",
provider: "AWS",
service: "Compute",
policy_set: "Superseded Compute Instances",
recommendation_type: "Usage Reduction"
)
###############################################################################
# Parameters
###############################################################################
parameter "param_email" do
type "list"
category "Policy Settings"
label "Email Addresses"
description "Email addresses of the recipients you wish to notify when new incidents are created"
default []
end
parameter "param_aws_account_number" do
type "string"
category "Policy Settings"
label "Account Number"
description "Leave blank; this is for automated use with Meta Policies. See README for more details."
default ""
end
parameter "param_instance_type" do
type "string"
category "Policy Settings"
label "Instance Type Category"
description "Instance Type Category to pick from for recommended instance types. See README for more information."
allowed_values "Regular", "Next Gen", "Burstable", "AMD"
default "Regular"
end
parameter "param_fallback_instance_type" do
type "string"
category "Policy Settings"
label "Fallback Instance Type Category"
description "Instance Type Category to pick from for recommended instance types if there are no valid recommendations for the primary category. Set to 'None' to have no fallback. See README for more information."
allowed_values "Regular", "Next Gen", "Burstable", "AMD", "None"
default "None"
end
parameter "param_min_savings" do
type "number"
category "Policy Settings"
label "Minimum Savings Threshold"
description "Minimum potential savings required to generate a recommendation"
min_value 0
default 0
end
parameter "param_exclusion_tags" do
type "list"
category "Filters"
label "Exclusion Tags"
description "Cloud native tags to ignore resources that you don't want to produce recommendations for. Enter the Key name to filter resources with a specific Key, regardless of Value, and enter Key==Value to filter resources with a specific Key:Value pair. Other operators and regex are supported; please see the README for more details."
default []
end
parameter "param_exclusion_tags_boolean" do
type "string"
category "Filters"
label "Exclusion Tags: Any / All"
description "Whether to filter instances containing any of the specified tags or only those that contain all of them. Only applicable if more than one value is entered in the 'Exclusion Tags' field."
allowed_values "Any", "All"
default "Any"
end
parameter "param_regions_allow_or_deny" do
type "string"
category "Filters"
label "Allow/Deny Regions"
description "Allow or Deny entered regions. See the README for more details"
allowed_values "Allow", "Deny"
default "Allow"
end
parameter "param_regions_list" do
type "list"
category "Filters"
label "Allow/Deny Regions List"
description "A list of allowed or denied regions. See the README for more details"
allowed_pattern /^([a-zA-Z-_]+-[a-zA-Z0-9-_]+-[0-9-_]+,*|)+$/
default []
end
parameter "param_automatic_action" do
type "list"
category "Actions"
label "Automatic Actions"
description "When this value is set, this policy will automatically take the selected action(s)"
allowed_values ["Change Instance Type"]
default []
end
###############################################################################
# Authentication
###############################################################################
credentials "auth_aws" do
schemes "aws", "aws_sts"
label "AWS"
description "Select the AWS Credential from the list"
tags "provider=aws"
aws_account_number $param_aws_account_number
end
credentials "auth_flexera" do
schemes "oauth2"
label "Flexera"
description "Select Flexera One OAuth2 credentials"
tags "provider=flexera"
end
###############################################################################
# Datasources & Scripts
###############################################################################
# Various data tables needed for later in the policy
datasource "ds_aws_instance_size_map" do
request do
host "raw.githubusercontent.com"
path "/flexera-public/policy_templates/master/data/aws/instance_types.json"
header "User-Agent", "RS Policies"
end
end
datasource "ds_aws_instance_cost_map" do
request do
host "raw.githubusercontent.com"
path "/flexera-public/policy_templates/master/data/aws/aws_ec2_pricing.json"
header "User-Agent", "RS Policies"
end
end
# Get applied policy metadata for use later
datasource "ds_applied_policy" do
request do
auth $auth_flexera
host rs_governance_host
path join(["/api/governance/projects/", rs_project_id, "/applied_policies/", policy_id])
header "Api-Version", "1.0"
end
end
# Get region-specific Flexera API endpoints
datasource "ds_flexera_api_hosts" do
run_script $js_flexera_api_hosts, rs_optima_host
end
script "js_flexera_api_hosts", type: "javascript" do
parameters "rs_optima_host"
result "result"
code <<-EOS
host_table = {
"api.optima.flexeraeng.com": {
flexera: "api.flexera.com",
fsm: "api.fsm.flexeraeng.com"
},
"api.optima-eu.flexeraeng.com": {
flexera: "api.flexera.eu",
fsm: "api.fsm-eu.flexeraeng.com"
},
"api.optima-apac.flexeraeng.com": {
flexera: "api.flexera.au",
fsm: "api.fsm-apac.flexeraeng.com"
}
}
result = host_table[rs_optima_host]
EOS
end
# Get AWS account info
datasource "ds_cloud_vendor_accounts" do
request do
auth $auth_flexera
host val($ds_flexera_api_hosts, 'flexera')
path join(["/finops-analytics/v1/orgs/", rs_org_id, "/cloud-vendor-accounts"])
header "Api-Version", "1.0"
end
result do
encoding "json"
collect jmes_path(response, "values[*]") do
field "id", jmes_path(col_item, "aws.accountId")
field "name", jmes_path(col_item, "name")
field "tags", jmes_path(col_item, "tags")
end
end
end
datasource "ds_get_caller_identity" do
request do
auth $auth_aws
host "sts.amazonaws.com"
path "/"
query "Action", "GetCallerIdentity"
query "Version", "2011-06-15"
header "User-Agent", "RS Policies"
end
result do
encoding "xml"
collect xpath(response, "//GetCallerIdentityResponse/GetCallerIdentityResult") do
field "account", xpath(col_item, "Account")
end
end
end
datasource "ds_aws_account" do
run_script $js_aws_account, $ds_cloud_vendor_accounts, $ds_get_caller_identity
end
script "js_aws_account", type:"javascript" do
parameters "ds_cloud_vendor_accounts", "ds_get_caller_identity"
result "result"
code <<-EOS
result = _.find(ds_cloud_vendor_accounts, function(account) {
return account['id'] == ds_get_caller_identity[0]['account']
})
// This is in case the API does not return the relevant account info
if (result == undefined) {
result = {
id: ds_get_caller_identity[0]['account'],
name: "",
tags: {}
}
}
EOS
end
datasource "ds_billing_centers" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/analytics/orgs/", rs_org_id, "/billing_centers"])
query "view", "allocation_table"
header "Api-Version", "1.0"
header "User-Agent", "RS Policies"
ignore_status [403]
end
result do
encoding "json"
collect jmes_path(response, "[*]") do
field "href", jmes_path(col_item, "href")
field "id", jmes_path(col_item, "id")
field "name", jmes_path(col_item, "name")
field "parent_id", jmes_path(col_item, "parent_id")
end
end
end
# Gather top level billing center IDs for when we pull cost data
datasource "ds_top_level_bcs" do
run_script $js_top_level_bcs, $ds_billing_centers
end
script "js_top_level_bcs", type: "javascript" do
parameters "ds_billing_centers"
result "result"
code <<-EOS
filtered_bcs = _.filter(ds_billing_centers, function(bc) {
return bc['parent_id'] == null || bc['parent_id'] == undefined
})
result = _.compact(_.pluck(filtered_bcs, 'id'))
EOS
end
# Gather local currency info
datasource "ds_currency_reference" do
request do
host "raw.githubusercontent.com"
path "/flexera-public/policy_templates/master/data/currency/currency_reference.json"
header "User-Agent", "RS Policies"
end
end
datasource "ds_currency_code" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/bill-analysis/orgs/", rs_org_id, "/settings/currency_code"])
header "Api-Version", "0.1"
header "User-Agent", "RS Policies"
ignore_status [403]
end
result do
encoding "json"
field "id", jmes_path(response, "id")
field "value", jmes_path(response, "value")
end
end
datasource "ds_currency_target" do
run_script $js_currency_target, $ds_currency_reference, $ds_currency_code
end
script "js_currency_target", type:"javascript" do
parameters "ds_currency_reference", "ds_currency_code"
result "result"
code <<-EOS
// Default to USD if currency is not found
result = ds_currency_reference['USD']
if (ds_currency_code['value'] != undefined && ds_currency_reference[ds_currency_code['value']] != undefined) {
result = ds_currency_reference[ds_currency_code['value']]
}
EOS
end
# Branching logic:
# This datasource returns an empty array if the target currency is USD.
# This prevents ds_currency_conversion from running if it's not needed.
datasource "ds_conditional_currency_conversion" do
run_script $js_conditional_currency_conversion, $ds_currency_target
end
script "js_conditional_currency_conversion", type: "javascript" do
parameters "ds_currency_target"
result "result"
code <<-EOS
result = []
// Make the request only if the target currency is not USD
if (ds_currency_target['code'] != 'USD') {
result = [1]
}
EOS
end
datasource "ds_currency_conversion" do
# Only make a request if the target currency is not USD
iterate $ds_conditional_currency_conversion
request do
host "api.xe-auth.flexeraeng.com"
path "/prod/{proxy+}"
query "from", "USD"
query "to", val($ds_currency_target, 'code')
query "amount", "1"
# Ignore currency conversion if API has issues
ignore_status [400, 404, 502]
end
result do
encoding "json"
field "from", jmes_path(response, "from")
field "to", jmes_path(response, "to")
field "amount", jmes_path(response, "amount")
field "year", jmes_path(response, "year")
end
end
datasource "ds_currency" do
run_script $js_currency, $ds_currency_target, $ds_currency_conversion
end
script "js_currency", type:"javascript" do
parameters "ds_currency_target", "ds_currency_conversion"
result "result"
code <<-EOS
result = ds_currency_target
result['exchange_rate'] = 1
if (ds_currency_conversion.length > 0) {
currency_code = ds_currency_target['code']
current_month = parseInt(new Date().toISOString().split('-')[1])
conversion_block = _.find(ds_currency_conversion[0]['to'][currency_code], function(item) {
return item['month'] == current_month
})
if (conversion_block != undefined) {
result['exchange_rate'] = conversion_block['monthlyAverage']
}
}
EOS
end
datasource "ds_describe_regions" do
request do
auth $auth_aws
host "ec2.amazonaws.com"
path "/"
query "Action", "DescribeRegions"
query "Version", "2016-11-15"
query "Filter.1.Name", "opt-in-status"
query "Filter.1.Value.1", "opt-in-not-required"
query "Filter.1.Value.2", "opted-in"
# Header X-Meta-Flexera has no affect on datasource query, but is required for Meta Policies
# Forces `ds_is_deleted` datasource to run first during policy execution
header "Meta-Flexera", val($ds_is_deleted, "path")
end
result do
encoding "xml"
collect xpath(response, "//DescribeRegionsResponse/regionInfo/item", "array") do
field "region", xpath(col_item, "regionName")
end
end
end
datasource "ds_regions" do
run_script $js_regions, $ds_describe_regions, $param_regions_list, $param_regions_allow_or_deny
end
script "js_regions", type:"javascript" do
parameters "ds_describe_regions", "param_regions_list", "param_regions_allow_or_deny"
result "result"
code <<-EOS
allow_deny_test = { "Allow": true, "Deny": false }
if (param_regions_list.length > 0) {
result = _.filter(ds_describe_regions, function(item) {
return _.contains(param_regions_list, item['region']) == allow_deny_test[param_regions_allow_or_deny]
})
} else {
result = ds_describe_regions
}
EOS
end
datasource "ds_instance_sets" do
iterate $ds_regions
request do
auth $auth_aws
host join(['ec2.', val(iter_item, 'region'), '.amazonaws.com'])
path '/'
query 'Action', 'DescribeInstances'
query 'Version', '2016-11-15'
header 'User-Agent', 'RS Policies'
header 'Content-Type', 'text/xml'
end
result do
encoding "xml"
collect xpath(response, "//DescribeInstancesResponse/reservationSet/item", "array") do
field "instances_set" do
collect xpath(col_item, "instancesSet/item", "array") do
field "region", val(iter_item, "region")
field "instanceId", xpath(col_item, "instanceId")
field "ipAddress", xpath(col_item, "ipAddress")
field "ipv6Address", xpath(col_item, "ipv6Address")
field "imageId", xpath(col_item, "imageId")
field "resourceType", xpath(col_item, "instanceType")
field "platform", xpath(col_item, "platformDetails")
field "privateDnsName", xpath(col_item, "privateDnsName")
field "launchTime", xpath(col_item, "launchTime")
field "tags" do
collect xpath(col_item, "tagSet/item", "array") do
field "key", xpath(col_item, "key")
field "value", xpath(col_item, "value")
end
end
end
end
end
end
end
datasource "ds_instances" do
run_script $js_instances, $ds_instance_sets, $param_exclusion_tags, $param_exclusion_tags_boolean
end
script "js_instances", type: "javascript" do
parameters "ds_instance_sets", "param_exclusion_tags", "param_exclusion_tags_boolean"
result "result"
code <<-EOS
comparators = _.map(param_exclusion_tags, function(item) {
if (item.indexOf('==') != -1) {
return { comparison: '==', key: item.split('==')[0], value: item.split('==')[1], string: item }
}
if (item.indexOf('!=') != -1) {
return { comparison: '!=', key: item.split('!=')[0], value: item.split('!=')[1], string: item }
}
if (item.indexOf('=~') != -1) {
value = item.split('=~')[1]
regex = new RegExp(value.slice(1, value.length - 1))
return { comparison: '=~', key: item.split('=~')[0], value: regex, string: item }
}
if (item.indexOf('!~') != -1) {
value = item.split('!~')[1]
regex = new RegExp(value.slice(1, value.length - 1))
return { comparison: '!~', key: item.split('!~')[0], value: regex, string: item }
}
// If = is present but none of the above are, assume user error and that the user intended ==
if (item.indexOf('=') != -1) {
return { comparison: '==', key: item.split('=')[0], value: item.split('=')[1], string: item }
}
// Assume we're just testing for a key if none of the comparators are found
return { comparison: 'key', key: item, value: null, string: item }
})
result = []
_.each(ds_instance_sets, function(item) {
if (param_exclusion_tags.length > 0) {
filtered_instances = _.reject(item['instances_set'], function(resource) {
resource_tags = {}
if (typeof(resource['tags']) == 'object') {
_.each(resource['tags'], function(tag) {
resource_tags[tag['key']] = tag['value']
})
}
// Store a list of found tags
found_tags = []
_.each(comparators, function(comparator) {
comparison = comparator['comparison']
value = comparator['value']
string = comparator['string']
resource_tag = resource_tags[comparator['key']]
if (comparison == 'key' && resource_tag != undefined) { found_tags.push(string) }
if (comparison == '==' && resource_tag == value) { found_tags.push(string) }
if (comparison == '!=' && resource_tag != value) { found_tags.push(string) }
if (comparison == '=~') {
if (resource_tag != undefined && value.test(resource_tag)) { found_tags.push(string) }
}
if (comparison == '!~') {
if (resource_tag == undefined) { found_tags.push(string) }
if (resource_tag != undefined && value.test(resource_tag)) { found_tags.push(string) }
}
})
all_tags_found = found_tags.length == comparators.length
any_tags_found = found_tags.length > 0 && param_exclusion_tags_boolean == 'Any'
return all_tags_found || any_tags_found
})
result = result.concat(filtered_instances)
} else {
result = result.concat(item['instances_set'])
}
})
EOS
end
datasource "ds_instance_costs" do
request do
run_script $js_instance_costs, $ds_aws_account, $ds_top_level_bcs, rs_org_id, rs_optima_host
end
result do
encoding "json"
collect jmes_path(response, "rows[*]") do
field "resourceId", jmes_path(col_item, "dimensions.resource_id")
field "billing_center_id", jmes_path(col_item, "dimensions.billing_center_id")
field "operating_system", jmes_path(col_item, "dimensions.operating_system")
field "purchase_option", jmes_path(col_item, "dimensions.purchase_option")
field "service", jmes_path(col_item, "dimensions.service")
field "cost", jmes_path(col_item, "metrics.cost_amortized_unblended_adj")
end
end
end
script "js_instance_costs", type: "javascript" do
parameters "ds_aws_account", "ds_top_level_bcs", "rs_org_id", "rs_optima_host"
result "request"
code <<-EOS
end_date = new Date()
end_date.setDate(end_date.getDate() - 2)
end_date = end_date.toISOString().split('T')[0]
start_date = new Date()
start_date.setDate(start_date.getDate() - 3)
start_date = start_date.toISOString().split('T')[0]
var request = {
auth: "auth_flexera",
host: rs_optima_host,
verb: "POST",
path: "/bill-analysis/orgs/" + rs_org_id + "/costs/select",
body_fields: {
dimensions: ["resource_id", "billing_center_id", "operating_system", "purchase_option", "service"],
granularity: "day",
start_at: start_date,
end_at: end_date,
metrics: ["cost_amortized_unblended_adj"],
billing_center_ids: ds_top_level_bcs,
limit: 100000,
filter: {
type: "and",
expressions: [
{
dimension: "service",
type: "equal",
value: "AmazonEC2"
},
{
dimension: "resource_type",
type: "equal",
value: "Compute Instance"
},
{
dimension: "vendor_account",
type: "equal",
value: ds_aws_account['id']
},
{
type: "not",
expression: {
dimension: "adjustment_name",
type: "substring",
substring: "Shared"
}
}
]
}
},
headers: {
'User-Agent': "RS Policies",
'Api-Version': "1.0"
},
ignore_status: [400]
}
EOS
end
datasource "ds_instance_costs_grouped" do
run_script $js_instance_costs_grouped, $ds_instance_costs, $ds_billing_centers
end
script "js_instance_costs_grouped", type: "javascript" do
parameters "ds_instance_costs", "ds_billing_centers"
result "result"
code <<-EOS
bc_object = {}
_.each(ds_billing_centers, function(bc) {
bc_object[bc['id']] = bc['name']
})
// Multiple a single day's cost by the average number of days in a month.
// The 0.25 is to account for leap years for extra precision.
cost_multiplier = 365.25 / 12
// Group cost data by resourceId for later use
result = {}
_.each(ds_instance_costs, function(item) {
id = item['resourceId'].toLowerCase()
if (result[id] == undefined) { result[id] = { cost: 0 } }
result[id]['cost'] += item['cost'] * cost_multiplier
result[id]['operating_system'] = item['operating_system']
result[id]['billing_center'] = bc_object[item['billing_center_id']]
result[id]['purchase_option'] = item['purchase_option']
})
EOS
end
datasource "ds_superseded_instances" do
run_script $js_superseded_instances, $ds_instances, $ds_instance_costs_grouped, $ds_aws_instance_size_map, $ds_aws_instance_cost_map, $ds_currency_conversion, $ds_currency, $ds_aws_account, $ds_applied_policy, $param_instance_type, $param_fallback_instance_type, $param_min_savings
end
script "js_superseded_instances", type: "javascript" do
parameters "ds_instances", "ds_instance_costs_grouped", "ds_aws_instance_size_map", "ds_aws_instance_cost_map", "ds_currency_conversion", "ds_currency", "ds_aws_account", "ds_applied_policy", "param_instance_type", "param_fallback_instance_type", "param_min_savings"
result "result"
code <<-'EOS'
// Used for formatting numbers to look pretty
function formatNumber(number, separator) {
formatted_number = "0"
if (number) {
formatted_number = (Math.round(number * 100) / 100).toString().split(".")[0]
if (separator) {
withSeparator = ""
for (var i = 0; i < formatted_number.length; i++) {
if (i > 0 && (formatted_number.length - i) % 3 == 0) { withSeparator += separator }
withSeparator += formatted_number[i]
}
formatted_number = withSeparator
}
decimal = (Math.round(number * 100) / 100).toString().split(".")[1]
if (decimal) { formatted_number += "." + decimal }
}
return formatted_number
}
result = []
total_savings = 0.0
_.each(ds_instances, function(instance) {
id = instance['instanceId'].toLowerCase()
instance_type = instance['resourceType']
superseded_type = null
instance_type_price = null
superseded_type_price = null
savings = 0.0
hourly_cost_multiplier = 365.25 / 12 * 24
cost = null
operating_system = null
billing_center = null
purchase_option = null
if (ds_instance_costs_grouped[id] != undefined) {
cost = ds_instance_costs_grouped[id]['cost']
operating_system = ds_instance_costs_grouped[id]['operating_system']
billing_center = ds_instance_costs_grouped[id]['billing_center']
purchase_option = ds_instance_costs_grouped[id]['purchase_option']
}
type_table = {
"Regular": "regular",
"Next Gen": "next_gen",
"Burstable": "burstable",
"AMD": "amd"
}
superseded_parameter = type_table[param_instance_type]
if (ds_aws_instance_size_map[instance_type] != undefined) {
superseded_table = ds_aws_instance_size_map[instance_type]['superseded']
if (typeof(superseded_table) == 'object') {
superseded_type = superseded_table[superseded_parameter]
recommendationType = param_instance_type
}
}
if ((typeof(superseded_type) != 'string' || superseded_type != '') && param_fallback_instance_type != "None") {
superseded_parameter = type_table[param_fallback_instance_type]
if (ds_aws_instance_size_map[instance_type] != undefined) {
superseded_table = ds_aws_instance_size_map[instance_type]['superseded']
if (typeof(superseded_table) == 'object') {
superseded_type = superseded_table[superseded_parameter]
recommendationType = param_fallback_instance_type
}
}
}
if (typeof(superseded_type) == 'string' && superseded_type != '') {
if (typeof(operating_system) == 'string') {
if (ds_aws_instance_cost_map[instance['region']] != undefined) {
instance_type_cost_map = ds_aws_instance_cost_map[instance['region']][instance_type]
if (instance_type_cost_map != undefined) {
instance_type_price_map = instance_type_cost_map[operating_system]
}
if (instance_type_price_map != undefined) {
instance_type_price = instance_type_price_map['pricePerUnit']
}
superseded_type_cost_map = ds_aws_instance_cost_map[instance['region']][superseded_type]
if (superseded_type_cost_map != undefined) {
superseded_type_price_map = superseded_type_cost_map[operating_system]
}
if (superseded_type_price_map != undefined) {
superseded_type_price = superseded_type_price_map['pricePerUnit']
}
}
}
if (typeof(instance_type_price) == 'number' && typeof(superseded_type_price) == 'number') {
instance_type_price *= ds_currency['exchange_rate'] * hourly_cost_multiplier
superseded_type_price *= ds_currency['exchange_rate'] * hourly_cost_multiplier
savings = instance_type_price - superseded_type_price
}
if (savings >= param_min_savings) {
total_savings += savings
tags = []
resourceName = ""
if (instance['tags'] != null && instance['tags'] != undefined) {
_.each(instance['tags'], function(tag) {
tags.push([tag['key'], tag['value']].join('='))
if (tag['key'].toLowerCase() == 'name') {
resourceName = tag['value']
}
})
}
savings = parseFloat(savings.toFixed(3))
if (typeof(cost) == 'number') {
cost = parseFloat(cost.toFixed(3))
}
if (typeof(instance_type_price) == 'number') {
instance_type_price = parseFloat(instance_type_price.toFixed(3))
}
if (typeof(superseded_type_price) == 'number') {
superseded_type_price = parseFloat(superseded_type_price.toFixed(3))
}
recommendationDetails = [
"Change instance type of EC2 instance ", instance["instanceId"], " ",
"in AWS Account ", ds_aws_account['name'], " ",
"(", ds_aws_account['id'], ") ",
"from ", instance["resourceType"], " ",
"to ", superseded_type
].join('')
hostname = null
if (typeof(instance['privateDnsName']) == 'string') {
hostname = instance['privateDnsName'].split('.')[0]
}
resourceARN = "arn:aws:ec2:" + instance['region'] + ":" + ds_aws_account['id'] + ":instance/" + instance['instanceId']
result.push({
region: instance['region'],
resourceID: instance['instanceId'],
resourceARN: resourceARN,
ipAddress: instance['ipAddress'],
ipv6Address: instance['ipv6Address'],
imageId: instance['imageId'],
resourceType: instance['resourceType'],
platform: instance['platform'],
privateDnsName: instance['privateDnsName'],
launchTime: instance['launchTime'],
osType: operating_system,
billing_center: billing_center,
purchase_option: purchase_option,
service: "AmazonEC2",
tags: tags.join(', '),
accountID: ds_aws_account['id'],
accountName: ds_aws_account['name'],
hostname: hostname,
recommendationDetails: recommendationDetails,
newResourceType: superseded_type,
resourceName: resourceName,
cost: cost,
instance_type_price: instance_type_price,
superseded_type_price: superseded_type_price,
savings: savings,
savingsCurrency: ds_currency['symbol'],
recommendationType: recommendationType,
policy_name: ds_applied_policy['name'],
total_savings: "",
message: ""
})
}
}
})
// Sort by descending order of savings value
result = _.sortBy(result, function(item) { return item['savings'] * -1 })
// Message for incident detailed template
savings_message = [
ds_currency['symbol'], ' ',
formatNumber(parseFloat(total_savings).toFixed(2), ds_currency['t_separator'])
].join('')
total_instances = ds_instances.length.toString()
total_superseded = result.length.toString()
superseded_percentage = (total_superseded / total_instances * 100).toFixed(2).toString() + '%'
instance_noun = "instance"
if (Number(total_instances) > 1) { instance_noun = "instances" }
superseded_verb = "is"
if (Number(total_superseded) > 1) { superseded_verb = "are" }
findings = [
"Out of ", total_instances, " EC2 ", instance_noun, " analyzed, ",
total_superseded, " (", superseded_percentage,
") ", superseded_verb, " superseded and recommended for an instance type change. "
].join('')
if (param_fallback_instance_type == "None") {
findings += "All recommendations are for '", param_instance_type, "' instance types.\n\n"
} else {
findings += "All recommendations are for '", param_instance_type, "' instance types when available with '", param_fallback_instance_type, "' as a fallback instance type when the primary type is not an option.\n\n"
}
disclaimer = "The above settings can be modified by editing the applied policy and changing the appropriate parameters.\n\n"
api_disclaimer = ""
if (ds_currency_conversion['to'] == undefined && ds_currency['code'] != 'USD') {
api_disclaimer = "\n\nSavings values are in USD due to a malfunction with Flexera's internal currency conversion API. Please contact Flexera support to report this issue."
}
savings_disclaimer = "Savings are estimated based on list price and may not reflect credits or discounts. "
if (ds_currency['code'] != "USD" && api_disclaimer == "") {
savings_disclaimer += "List prices were converted from USD using current exchange rates."
}
// Dummy entry to ensure the check statement in validation always runs at least once
result.push({
region: "",
resourceID: "",
ipAddress: "",
ipv6Address: "",
imageId: "",
resourceType: "",
platform: "",
privateDnsName: "",
launchTime: "",
osType: "",
billing_center: "",
purchase_option: "",
service: "",
tags: "",
accountID: "",
accountName: "",
hostname: "",
recommendationDetails: "",
newResourceType: "",
resourceName: "",
cost: "",
instance_type_price: "",
superseded_type_price: "",
savings: "",
savingsCurrency: "",
recommendationType: "",
policy_name: "",
total_savings: "",
message: ""
})
result[0]['total_savings'] = savings_message
result[0]['message'] = findings + disclaimer + savings_disclaimer + api_disclaimer
EOS
end
###############################################################################
# Policy
###############################################################################
policy "pol_superseded_instances" do
validate_each $ds_superseded_instances do
summary_template "{{ with index data 0 }}{{ .policy_name }}{{ end }}: {{ len data }} AWS Potentially Superseded EC2 Instances Found"
detail_template <<-'EOS'
**Potential Monthly Savings:** {{ with index data 0 }}{{ .total_savings }}{{ end }}
{{ with index data 0 }}{{ .message }}{{ end }}
EOS
check logic_or($ds_parent_policy_terminated, eq(val(item, "resourceID"), ""))
escalate $esc_email
escalate $esc_change_type
hash_exclude "message", "total_savings", "resourceName", "tags", "cost", "instance_type_price", "superseded_type_price", "savings", "savingsCurrency"
export do
resource_level true
field "accountID" do
label "Account ID"
end
field "accountName" do
label "Account Name"
end
field "resourceID" do
label "Resource ID"
end
field "resourceName" do
label "Resource Name"
end
field "tags" do
label "Resource Tags"
end
field "recommendationDetails" do
label "Recommendation"
end
field "resourceType" do
label "Instance Size"
end
field "newResourceType" do
label "Recommended Instance Size"
end
field "recommendationType" do
label "Recommendation Type"
end
field "region" do