-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaner.js
130 lines (111 loc) · 3.64 KB
/
cleaner.js
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
"use strict";
const AWS = require("aws-sdk");
const lambda = new AWS.Lambda();
const cloudwatchlogs = new AWS.CloudWatchLogs();
exports.handler = async (event) => {
console.log("Received event: " + JSON.stringify(event, null, 2));
try {
// retrieve lists is parallel
let results = await Promise.all([buildListOfFunctionNames(), buildListOfGroupNames()]);
let functionNames = results[0];
let groupNames = results[1];
console.log("Found " + functionNames.length + " functions.");
console.log("Found " + groupNames.length + " log groups.");
// do the processing and return result
var processingResults = await processLogGroups(functionNames, groupNames);
console.log("Returning result: " + JSON.stringify(processingResults, null, 2));
return processingResults;
} catch (e) {
throw new Error("Failed to clean log groups: " + e.message);
}
};
var buildListOfFunctionNames = async () => {
try {
let functionNames = [];
let nextMarker = null;
let params = {
MaxItems: 25
};
do {
if (nextMarker !== null) {
params.Marker = nextMarker;
}
let data = await lambda.listFunctions(params).promise();
//console.log("listFunctions response: " + JSON.stringify(data, null, 2));
for (var i = 0; i < data.Functions.length; i++) {
functionNames.push(data.Functions[i].FunctionName);
}
nextMarker = data.NextMarker;
} while (nextMarker !== null && nextMarker !== undefined);
return functionNames;
} catch (e) {
throw e;
}
};
var buildListOfGroupNames = async () => {
try {
let groupNames = [];
let nextToken = null;
let params = {
logGroupNamePrefix: "/aws/lambda/",
limit: 25
};
do {
if (nextToken !== null) {
params.nextToken = nextToken;
}
let data = await cloudwatchlogs.describeLogGroups(params).promise();
//console.log("describeLogGroups response: " + JSON.stringify(data, null, 2));
for (var i = 0; i < data.logGroups.length; i++) {
groupNames.push(data.logGroups[i].logGroupName);
}
nextToken = data.nextToken;
} while (nextToken !== null && nextToken !== undefined);
return groupNames;
} catch (e) {
throw e;
}
};
var processLogGroups = async (functionNames, groupNames) => {
try {
let groupsProcessed = 0;
let groupsIgnored = 0;
let groupsDeleted = 0;
let groupsFailed = 0;
// iterate through log groups and look for associated lambda function
for (let i = 0; i < groupNames.length; i++) {
groupsProcessed++;
let groupName = groupNames[i];
// strip prefix from group name
let lambdaName = groupName.substring(12);
// look for name in list of functions
var lambdaExists = functionNames.includes(lambdaName);
// if function is missing, delete the log group
if (lambdaExists) {
groupsIgnored++;
console.log("Ignoring group " + groupName + " as the Lambda function exists");
} else {
console.log("Deleting group " + groupName + " as the Lambda function no longer exists...");
var deleteGroupParams = {
logGroupName: groupName
};
try {
await cloudwatchlogs.deleteLogGroup(deleteGroupParams).promise();
groupsDeleted++;
} catch (deleteError) {
console.log("Failed to delete group " + groupName);
groupsFailed++;
}
}
}
// build and return result object
return {
groupsProcessed: groupsProcessed,
groupsIgnored: groupsIgnored,
groupsDeleted: groupsDeleted,
groupsFailed: groupsFailed
};
} catch (e) {
throw e;
}
};