-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathaws_savings_realized.pt
421 lines (364 loc) · 15.3 KB
/
aws_savings_realized.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
name "AWS Savings Realized From Rate Reduction Purchases"
rs_pt_ver 20180301
type "policy"
short_description "Reports savings realized from AWS rate reduction purchases, such as Reserved Instances, Savings Plan, and Spot Instance purchases. See the [README](https://github.com/flexera-public/policy_templates/tree/master/cost/aws/savings_realized/) 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 "monthly"
info(
version: "4.0.1",
provider: "AWS",
service: "Compute",
policy_set: "Reserved Instance",
hide_skip_approvals: "true"
)
###############################################################################
# Parameters
###############################################################################
parameter "param_email" do
type "list"
category "Policy Settings"
label "Email Addresses"
description "A list of email addresses to send the report to."
default []
end
parameter "param_months_back" do
type "number"
category "Policy Settings"
label "Months Back"
description "How many months to go back in the report. For example, set this to '6' to report on the previous 6 months."
min_value 1
max_value 12
default 6
end
parameter "param_chart_type" do
type "string"
category "Policy Settings"
label "Chart Type"
description "The type of bar chart to use in the report."
allowed_values "Grouped Bar Chart", "Stacked Bar Chart"
default "Grouped Bar Chart"
end
parameter "param_bc_allow_or_deny" do
type "string"
category "Filters"
label "Allow/Deny Billing Centers"
description "Allow or Deny entered Billing Centers."
allowed_values "Allow", "Deny"
default "Allow"
end
parameter "param_bc_list" do
type "list"
category "Filters"
label "Allow/Deny Billing Center List"
description "A list of allowed or denied Billing Center names/IDs. Leave blank to report on the entire Flexera organization."
default []
end
###############################################################################
# Authentication
###############################################################################
credentials "auth_flexera" do
schemes "oauth2"
label "Flexera"
description "Select Flexera One OAuth2 credentials"
tags "provider=flexera"
end
###############################################################################
# Datasources & Scripts
###############################################################################
# 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
datasource "ds_billing_centers" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/analytics/orgs/", rs_org_id, "/billing_centers"])
header "Api-Version", "1.0"
header "User-Agent", "RS Policies"
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
datasource "ds_billing_centers_filtered" do
run_script $js_billing_centers_filtered, $ds_billing_centers, $param_bc_allow_or_deny, $param_bc_list
end
script "js_billing_centers_filtered", type: "javascript" do
parameters "ds_billing_centers", "param_bc_allow_or_deny", "param_bc_list"
result "result"
code <<-EOS
allow_deny_test = { "Allow": true, "Deny": false }
if (param_bc_list.length > 0) {
billing_centers = _.filter(ds_billing_centers, function(item) {
id_found = _.contains(param_bc_list, item['id']) == allow_deny_test[param_bc_allow_or_deny]
name_found = _.contains(param_bc_list, item['name']) == allow_deny_test[param_bc_allow_or_deny]
return id_found || name_found
})
// Check for conflicting parents/children and remove children if present
bc_ids = _.compact(_.pluck(billing_centers, 'id'))
bad_children = _.filter(ds_billing_centers, function(bc) { return _.contains(bc_ids, bc['parent_id']) })
bad_children_ids = _.pluck(bad_children, 'id')
// Create final result with the bad children removed
final_list = _.reject(billing_centers, function(bc) { return _.contains(bad_children_ids, bc['id']) })
} else {
// If we're not filtering at all, just grab all of the top level billing centers
final_list = _.filter(ds_billing_centers, function(bc) {
return bc['parent_id'] == null || bc['parent_id'] == undefined
})
}
result = _.compact(_.pluck(final_list, 'id'))
EOS
end
datasource "ds_aggregated_costs" do
request do
run_script $js_aggregated_costs, $ds_billing_centers_filtered, $param_months_back, rs_org_id, rs_optima_host
end
result do
encoding "json"
collect jmes_path(response, "rows") do
field "purchase_option", jmes_path(col_item, "dimensions.purchase_option")
field "cost", jmes_path(col_item, "metrics.cost_amortized_unblended_adj")
field "savings", jmes_path(col_item, "metrics.savings_rate_reduction_amortized_adj")
field "month", jmes_path(col_item, "timestamp")
end
end
end
script "js_aggregated_costs", type: "javascript" do
parameters "ds_billing_centers_filtered", "param_months_back", "rs_org_id", "rs_optima_host"
result "request"
code <<-EOS
// Get Start and End dates
start_date = new Date()
start_date.setMonth(start_date.getMonth() - param_months_back)
start_date = start_date.toISOString().split('-')[0] + '-' + start_date.toISOString().split('-')[1]
end_date = new Date()
end_date = end_date.toISOString().split('-')[0] + '-' + end_date.toISOString().split('-')[1]
var request = {
auth: "auth_flexera",
host: rs_optima_host,
path: "/bill-analysis/orgs/" + rs_org_id + "/costs/aggregated",
verb: "POST",
headers: { "User-Agent": "RS Policies" },
body_fields: {
"billing_center_ids": ds_billing_centers_filtered,
"dimensions": [ "purchase_option" ],
"granularity": "month",
"metrics": [ "cost_amortized_unblended_adj", "savings_rate_reduction_amortized_adj" ],
"start_at": start_date,
"end_at": end_date,
"filter": {
"type": "and",
"expressions": [
{ "dimension": "vendor", "type": "equal", "value": "AWS" },
{
"type": "not",
"expression": {
"dimension": "purchase_option",
"type": "equal",
"value": "On Demand (Non-Discountable)"
}
}
]
}
}
}
EOS
end
datasource "ds_savings_realized" do
run_script $js_savings_realized, $ds_aggregated_costs
end
script "js_savings_realized", type: "javascript" do
parameters "ds_aggregated_costs"
result "result"
code <<-'EOS'
result = []
// Get list of Months
months = _.uniq(_.pluck(ds_aggregated_costs, "month"))
// Calculate total cost for each month
_.each(months, function(month) {
month_costs = _.filter(ds_aggregated_costs, function(item) { return item['month'] == month })
total_month_cost = _.reduce(_.pluck(month_costs, 'cost'), function(memo, num){ return memo + num }, 0)
result.push({
month: month.substring(0, 7),
cost: total_month_cost,
dimension: "Total Actual Spend On Discountable Compute"
})
})
// Group by Purchase Option
option_table = { "Reserved": "Reservations", "Savings Plan": "Savings Plans", "Spot": "Spot Instances" }
temp_result = _.map(ds_aggregated_costs, function(item) {
return {
month: item["month"],
savings_realized: item["savings"],
purchase_option: option_table[item["purchase_option"]]
}
})
// Clean out anything that isn't a rate reduction purchase
temp_result = _.reject(temp_result, function(item) { return item['purchase_option'] == undefined })
// Aggregate SAVINGS REALIZED costs for same purchase option for same month
purchase_options = _.compact(_.uniq(_.pluck(temp_result, "purchase_option"))).sort()
_.each(purchase_options, function(purchase_option) {
_.each(months, function(month) {
relevant_entries = _.filter(temp_result, function(item) {
return item["purchase_option"] == purchase_option && item["month"] == month
})
relevant_costs = _.pluck(relevant_entries, 'savings_realized')
savings_realized = _.reduce(relevant_costs, function(memo, num){ return memo + num }, 0)
result.push({
"month": month.substring(0, 7),
"cost": savings_realized,
"dimension": "Savings Realized On Compute " + purchase_option
})
})
})
// Sort by month
result = _.sortBy(result, "month")
EOS
end
datasource "ds_chart_data" do
run_script $js_chart_data, $ds_savings_realized, $ds_applied_policy, $param_chart_type
end
script "js_chart_data", type: "javascript" do
parameters "ds_savings_realized", "ds_applied_policy", "param_chart_type"
result "result"
code <<-EOS
result = []
// Get months
months = _.uniq(_.pluck(ds_savings_realized, "month"))
// Create report data
// (Concatenate savings realized and total costs into one object/row of data)
_.each(months, function(month) {
savings_realized_ri = 0.0
savings_realized_sp = 0.0
savings_realized_spot = 0.0
total_spend = 0.0
ri_entry = _.find(ds_savings_realized, function(item) {
return item['dimension'] == "Savings Realized On Compute Reservations" && item['month'] == month
})
sp_entry = _.find(ds_savings_realized, function(item) {
return item['dimension'] == "Savings Realized On Compute Savings Plans" && item['month'] == month
})
spot_entry = _.find(ds_savings_realized, function(item) {
return item['dimension'] == "Savings Realized On Compute Spot Instances" && item['month'] == month
})
total_spend_entry = _.find(ds_savings_realized, function(item) {
return item['dimension'] == "Total Actual Spend On Discountable Compute" && item['month'] == month
})
if (ri_entry != undefined) { savings_realized_ri = ri_entry['cost'] }
if (sp_entry != undefined) { savings_realized_sp = sp_entry['cost'] }
if (spot_entry != undefined) { savings_realized_spot = spot_entry['cost'] }
if (total_spend_entry != undefined) { total_spend = total_spend_entry['cost'] }
result.push({
"month": month,
"total_spend": Math.round(total_spend * 100) / 100,
"savings_realized_ri": Math.round(savings_realized_ri * 100) / 100,
"savings_realized_sp": Math.round(savings_realized_sp * 100) / 100,
"savings_realized_spot": Math.round(savings_realized_spot * 100) / 100,
"percentage_ri": Math.round((savings_realized_ri / (total_spend + savings_realized_ri)) * 100).toString() + "%",
"percentage_sp": Math.round((savings_realized_sp / (total_spend + savings_realized_sp)) * 100).toString() + "%",
"percentage_spot": Math.round((savings_realized_spot / (total_spend + savings_realized_spot)) * 100).toString() + "%"
})
})
// Group data by Cost Dimension
group_by_dimension = _.groupBy(ds_savings_realized, 'dimension')
// Determine chart type
chart_type = "cht=bvs"
if (param_chart_type == "Grouped Bar Chart") { chart_type = "cht=bvg" }
// Create chart axis labels
chart_axis_labels = "chxl=1:|" + months.join('|')
// Create legend
chart_legend = "chdl=" + _.keys(group_by_dimension).join('|')
// Calculate scale
max = (_.pluck(ds_savings_realized, 'savings_realized').sort().reverse()[0] * 1.1).toFixed(2)
min = (_.pluck(ds_savings_realized, 'savings_realized').sort()[0] * 0.9).toFixed(2)
chart_range = ["chxr=0", min, max].join(',')
// Create chart dataset
data_parts = _.map(group_by_dimension, function(dimension_list) {
return _.pluck(dimension_list, 'cost').join(',')
})
chart_data = "chd=t:" + data_parts.join('|')
// Whole Chart object
chart = {
chart_type: encodeURI(chart_type),
chart_size: encodeURI("chs=999x500"),
chart_data: encodeURI(chart_data),
chart_title: encodeURI("chtt=Savings Realized For Organization"),
chart_image: encodeURI("chof=.png"),
chart_y_axis: encodeURI("chxt=y,x"),
chart_axis_label: encodeURI(chart_axis_labels),
chart_axis_format: encodeURI("chxs=0N*cUSD0sz*|1,min40"),
chart_line_style: encodeURI("chls=3|3|3|3|3|3|3|3|3|3|3"),
chart_line_color: encodeURI("chco=1f5ab8,55b81f,fc9803,b28600,1192e8,009d9a,005d5d,007d79"),
chart_data_scale: encodeURI("chds=a"),
chart_legend: encodeURI(chart_legend),
chart_legend_size: encodeURI("chdls=000000,10"),
chart_legend_position: encodeURI("chdlp=b")
}
result[0]["chart_dimensions"] = chart
result[0]["policy_name"] = ds_applied_policy['name']
EOS
end
###############################################################################
# Policy
###############################################################################
policy "pol_savings_realized" do
validate_each $ds_chart_data do
summary_template "{{ with index data 0 }}{{ .policy_name }}{{ end }}"
detail_template <<-EOS
# Rate Reduction Savings Report
![Rate Reduction Savings Chart](https://api.image-charts-auth.flexeraeng.com/ic-function?rs_org_id={{ rs_org_id }}&rs_project_id={{ rs_project_id }}&{{ with index data 0 }}{{ .chart_dimensions.chart_data }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_size }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_type }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_image }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_title }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_y_axis }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_axis_label }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_line_style }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_line_color }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_data_scale }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_legend_size }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_legend }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_axis_format }}{{ end }}&{{ with index data 0 }}{{ .chart_dimensions.chart_legend_position }}{{ end }})
EOS
check eq(0, 1)
escalate $esc_email
export do
resource_level false
field "month" do
label "Month"
end
field "total_spend" do
label "Total Actual Spend On Discountable Compute"
end
field "savings_realized_ri" do
label "Savings Realized On Compute Reservations"
end
field "savings_realized_sp" do
label "Savings Realized On Compute Savings Plans"
end
field "savings_realized_spot" do
label "Savings Realized On Compute Spot Instances"
end
field "percentage_ri" do
label "Savings Rate from Reservation usage"
end
field "percentage_sp" do
label "Savings Rate from Savings Plan usage"
end
field "percentage_spot" do
label "Savings Rate from Spot Instance usage"
end
end
end
end
###############################################################################
# Escalations
###############################################################################
escalation "esc_email" do
automatic true
label "Send Email"
description "Send incident email"
email $param_email
end