-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathaws_savings_plan_expiration.pt
235 lines (214 loc) · 7.31 KB
/
aws_savings_plan_expiration.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
name "AWS Expiring Savings Plans"
rs_pt_ver 20180301
type "policy"
short_description "Reports on AWS Savings Plans that have or will soon expire. See the [README](https://github.com/flexera-public/policy_templates/tree/master/cost/aws/savings_plan/expiration) and [docs.flexera.com/flexera/EN/Automation](https://docs.flexera.com/flexera/EN/Automation/AutomationGS.htm) to learn more."
long_description ""
category "Cost"
severity "medium"
default_frequency "daily"
info(
version: "3.0.1",
provider: "AWS",
service: "Compute",
policy_set: "Savings Plans",
hide_skip_approvals: "true"
)
###############################################################################
# Parameters
###############################################################################
parameter "param_email" do
type "list"
category "Policy Settings"
label "Email Addresses"
description "A list of email addresses to notify."
default []
end
parameter "param_days_expiration" do
type "number"
category "Policy Settings"
label "Days Until Expiration"
description "The number of days until expiration to include a Savings Plan in the report. Set to '0' to only report expired Savings Plans."
min_value 0
default 15
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
###############################################################################
# Pagination
###############################################################################
pagination "pagination_aws" do
get_page_marker do
body_path jmes_path(response, "NextToken")
end
set_page_marker do
body_field "NextToken"
end
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_savings_plans" do
request do
auth $auth_aws
pagination $pagination_aws
verb "POST"
host "savingsplans.us-east-1.amazonaws.com"
path "/DescribeSavingsPlans"
header "User-Agent", "RS Policies"
header "Content-Type", "application/json"
end
result do
encoding "json"
collect jmes_path(response, "savingsPlans[*]") do
field "savingsPlanID", jmes_path(col_item, "savingsPlanId")
field "savingsPlanArn", jmes_path(col_item, "savingsPlanArn")
field "savingsPlanType", jmes_path(col_item, "savingsPlanType")
field "startDate", jmes_path(col_item, "start")
field "endDate", jmes_path(col_item, "end")
field "commitment", jmes_path(col_item, "commitment")
field "currency", jmes_path(col_item, "currency")
field "description", jmes_path(col_item, "description")
field "ec2InstanceFamily", jmes_path(col_item, "ec2InstanceFamily")
field "offeringID", jmes_path(col_item, "offeringId")
field "state", jmes_path(col_item, "state")
field "tags", jmes_path(col_item, "tags")
field "termDurationInSeconds", jmes_path(col_item, "termDurationInSeconds")
field "upfrontPaymentAmount", jmes_path(col_item, "upfrontPaymentAmount")
end
end
end
datasource "ds_expiring_savings_plans" do
run_script $js_expiring_savings_plans, $ds_savings_plans, $ds_applied_policy, $param_days_expiration
end
script "js_expiring_savings_plans", type: "javascript" do
parameters "ds_savings_plans", "ds_applied_policy", "param_days_expiration"
result "result"
code <<-EOS
result = []
today = new Date()
_.each(ds_savings_plans, function(plan) {
expiration_date = new Date(plan['endDate'])
days_until_expiry = (expiration_date - today) / 1000 / 60 / 60 / 24
if (days_until_expiry < 0) { days_until_expiry = 0 }
if (days_until_expiry < param_days_expiration || days_until_expiry == 0) {
tags = []
if (typeof(plan['tags']) == 'object') {
_.each(Object.keys(plan['tags']), function(key) {
tags.push([key, "=", plan['tags'][key]].join(''))
})
}
result.push({
resourceID: plan["savingsPlanID"],
savingsPlanArn: plan["savingsPlanArn"],
savingsPlanType: plan["savingsPlanType"],
startDate: new Date(plan['startDate']).toISOString().split("T")[0],
endDate: new Date(plan['endDate']).toISOString().split("T")[0],
commitment: plan["commitment"],
currency: plan["currency"],
description: plan["description"],
ec2InstanceFamily: plan["ec2InstanceFamily"],
offeringID: plan["offeringID"],
state: plan["state"],
termDurationInSeconds: plan["termDurationInSeconds"],
upfrontPaymentAmount: plan["upfrontPaymentAmount"],
term: Math.round(plan["termDurationInSeconds"] / 31536000 * 100) / 100,
days_until_expiry: Math.round(days_until_expiry),
tags: tags.join(", "),
policy_name: ds_applied_policy["name"]
})
}
})
EOS
end
###############################################################################
# Policy
###############################################################################
policy "pol_expiring_savings_plans" do
validate_each $ds_expiring_savings_plans do
summary_template "{{ with index data 0 }}{{ .policy_name }}{{ end }}: {{ len data }} Expiring AWS Savings Plans Found"
check eq(val(item, "resourceID"), "")
escalate $esc_email
hash_exclude "tags", "days_until_expiry"
export do
resource_level true
field "resourceID" do
label "Savings Plan ID"
end
field "savingsPlanArn" do
label "Savings Plan ARN"
end
field "savingsPlanType" do
label "Savings Plan Type"
end
field "term" do
label "Term Duration (Years)"
end
field "termDurationInSeconds" do
label "Term Duration (Seconds)"
end
field "start" do
label "Start Date"
end
field "end" do
label "End Date"
end
field "days_until_expiry" do
label "Days Until Expiration"
end
field "state" do
label "State"
end
field "commitment" do
label "Commitment"
end
field "offeringID" do
label "Offering ID"
end
field "ec2InstanceFamily" do
label "EC2 Instance Family"
end
field "description" do
label "Description"
end
field "tags" do
label "Tags"
end
field "id" do
label "ID"
path "resourceID"
end
end
end
end
###############################################################################
# Escalations
###############################################################################
escalation "esc_email" do
automatic true
label "Send Email"
description "Send incident email"
email $param_email
end