-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowUtilization2.js
More file actions
174 lines (145 loc) · 4.52 KB
/
LowUtilization2.js
File metadata and controls
174 lines (145 loc) · 4.52 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
/**
Node 8.10
This is part 2 of sending notifications for low utilized EC2 instances:
1. Get data from DynamoDB for instances over 90 days
2. Get their tags by region
3. Iterate over the instances in step 1 and attach their tags taken from step 2
4. Send emails using SES
**/
// Your own constants
const tableName = 'LowUtilTable';
const awsRegion = 'us-west-2';
const emailSource = "you@email.com"; // verified
const emailTo = ["person@owner.com"]; // array of addresses
const AWS = require('aws-sdk');
AWS.config.update({
region: awsRegion
})
exports.handler = async (event, context, callback) => {
// get all dynamoDB instances back
let dynaInstances = await dynaDB();
// separate by region and then
let regionInstances = parseDaysRegion(dynaInstances.Items);
// iterate through all regions listed in dynamo
let instanceTags = await Promise.all(Object.entries(regionInstances)
.map(async ([key, value]) => {
return await acquireTags(value, key);
})
)
// merge the region tags into one object
instanceTags = instanceTags.reduce((acc, cv) => {
for (let key in cv) {
acc[key] = cv[key]
}
return acc;
}, {})
// Iterate over instances over 90 days and attach their tags from instanceTags
let email90 = dynaInstances.Items
.filter(v => v.days >= 90 && v.days < 120)
.map(v => {
v['tags'] = instanceTags[v.instance]
return v
});
// Iterate over instances over 120 days and attach their tags from instanceTags
let stop120 = dynaInstances.Items
.filter(v => v.days >= 120 && v.days < 150)
.map(v => {
v['tags'] = instanceTags[v.instance]
return v
});
// Iterate over instances over 150 days and attach their tags from instanceTags
let terminate150 = dynaInstances.Items
.filter(v => v.days >= 150)
.map(v => {
v['tags'] = instanceTags[v.instance]
return v
});
// send to SES with template message
if (email90.length) await sesSend(email90, 1);
if (stop120.length) await sesSend(stop120, 2);
if (terminate150.length) await sesSend(terminate150, 3);
}
// send emails with instance types.
// use templates later
async function sesSend(arr, ec2type) {
const instanceCount = arr.length;
let templateType = ''
const SES = new AWS.SES({
apiVersion: '2010-12-01'
});
switch (ec2type) {
case 1:
templateType = 'LowUtilizationEC2_90'
break;
case 2:
templateType = 'LowUtilizationEC2_120'
break;
case 3:
templateType = 'LowUtilizationEC2_150'
break;
default:
templateType = 'LowUtilizationEC2_90'
}
const emailTemplate = {
"Source": emailSource,
"Template": templateType,
"Destination": {
"ToAddresses": [...emailTo]
},
"TemplateData": stringEscape({
data: arr,
instanceCount
})
}
let sendIt = await SES.sendTemplatedEmail(emailTemplate).promise();
console.log('Sending: ', sendIt)
};
// Escape and remove double escapes
function stringEscape(dat) {
return JSON.stringify(dat).replace(/\\/g, '\\');
}
// take in the array of instances in each region(localRegion) and call EC2 to get the tags back
// then sort them by the instanceId into an Object
async function acquireTags(arr, localRegion) {
const EC2instances = arr.map(v => v.instance);
const EC2 = new AWS.EC2({ apiVersion: '2016-11-15', region: localRegion });
let ec2Params = {
Filters: [
{
Name: "resource-id",
Values: [
...EC2instances
]
}
]
};
let getAWSTags = await EC2.describeTags(ec2Params).promise()
let instanceTags = arrayToObject2(getAWSTags.Tags, 'ResourceId')
return instanceTags;
}
// Filter instances within the days and put them into an object by region key
function parseDaysRegion(arr) {
const regionInstances = {};
const over90 = arr.filter(v => v.days >= 90);
over90.forEach((v) => {
let keyAZ = v.region
let region = keyAZ.replace(/[.abcd]$/, "")
regionInstances[region] = regionInstances[region] ? [...regionInstances[region], v] : [v]
})
return regionInstances
}
// Get our data from dynamoDB
async function dynaDB() {
const dynaClient = new AWS.DynamoDB.DocumentClient();
const scanParams = {
"TableName": tableName
};
return await dynaClient.scan(scanParams).promise();
}
// Convert array to object with a selected key, but value will be an array of objects
function arrayToObject2(arr, key) {
return arr.reduce((acc, cv) => {
acc[cv[`${key}`]] = acc[cv[`${key}`]] ? [...acc[cv[`${key}`]], cv] : [cv]
return acc
}, {})
}