-
Notifications
You must be signed in to change notification settings - Fork 99
/
delete_old_lambda_function_versions.js
98 lines (91 loc) · 3.28 KB
/
delete_old_lambda_function_versions.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
/**
* Deletes lambda function versions.
*
* We can use this script to delete old versions of all our Lambda functions at once.
*/
const awsConfigHelper = require('./util/awsConfigHelper');
const wait = require('./util/wait');
const AWS = require('aws-sdk');
const cli = require('cli');
const cliArgs = cli.parse({
region: ['r', 'AWS region', 'string'],
filterName: ['f', 'Pass filter name to filter Lambda functions', 'string'],
maxVersions: ['v', 'Maximum versions to keep: should be greater than 0', 'number', 3]
});
if (!cliArgs.region || cliArgs.maxVersions < 1) {
cli.getUsage();
}
let lambda;
const filterRegex = new RegExp(cliArgs.filterName);
async function getFunctionVersions(functionName) {
let functionVersions = [];
let isCompleted = false;
let marker = undefined;
while (!isCompleted) {
let response = await lambda.listVersionsByFunction({
FunctionName: functionName,
Marker: marker
}).promise();
marker = response.NextMarker;
if (response.Versions) {
functionVersions = functionVersions.concat(response.Versions);
isCompleted = marker === undefined || marker === null;
} else {
isCompleted = true;
}
await wait(500);
}
return functionVersions.sort((v1, v2) => {
if (v1.LastModified < v2.LastModified) {
return 1;
} else if (v1.LastModified === v2.LastModified) {
return 0
} else {
return -1
}
});
}
async function deleteFunctionVersions() {
await awsConfigHelper.updateConfig(cliArgs.region);
lambda = new AWS.Lambda();
let isCompleted = false;
let nextToken = undefined;
while (!isCompleted) {
try {
const response = await lambda.listFunctions({
Marker: nextToken
}).promise();
if (response.Functions) {
for (let i = 0; i < response.Functions.length; i++) {
const fn = response.Functions[i];
if (cliArgs.filterName && !fn.FunctionName.match(filterRegex)) {
continue;
}
const functionVersions = await getFunctionVersions(fn.FunctionName);
if (functionVersions.length <= cliArgs.maxVersions) {
continue;
}
for (let i = cliArgs.maxVersions; i < functionVersions.length; i++) {
console.log(`Deleting version: ${functionVersions[i].Version} for function: ${functionVersions[i].FunctionName}`);
await lambda.deleteFunction({
FunctionName: functionVersions[i].FunctionName,
Qualifier: functionVersions[i].Version
}).promise();
await wait(1000);
}
}
nextToken = response.NextMarker;
isCompleted = !nextToken;
} else {
isCompleted = true
}
} catch (error) {
if (error.code === 'ThrottlingException') {
await wait(2000);
} else {
throw error;
}
}
}
}
deleteFunctionVersions();