This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathcleanup-lambda.ts
134 lines (121 loc) · 3.14 KB
/
cleanup-lambda.ts
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
#!/usr/bin/env node
// Cleanup Lambda code adapted from https://github.com/davidmenger/cleanup-lambda-versions/blob/master/src/cleanupVersions.js
import * as AWS from "aws-sdk";
import {
ListFunctionsResponse,
ListVersionsByFunctionResponse
} from "aws-sdk/clients/lambda";
function listLambdaFunctions(
lambda: AWS.Lambda,
nextMarker: string | undefined = undefined
): Promise<ListFunctionsResponse> {
return new Promise((resolve, reject) => {
const opts = {
MaxItems: 999
};
if (nextMarker) {
Object.assign(opts, { Marker: nextMarker });
}
lambda.listFunctions(opts, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
function listLambdaVersions(
lambda: AWS.Lambda,
fnName: string
): Promise<ListVersionsByFunctionResponse> {
return new Promise((resolve, reject) => {
lambda.listVersionsByFunction(
{
FunctionName: fnName,
MaxItems: 50
},
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
}
);
});
}
function removeLambdaVersion(
lambda: AWS.Lambda,
fnName: string,
version: string
): Promise<unknown> {
return new Promise((resolve, reject) => {
lambda.deleteFunction(
{ FunctionName: fnName, Qualifier: version },
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
}
);
});
}
async function cleanupVersions(
region: string,
removeIt = false,
nextMarker: string | undefined = undefined
) {
const lambda: AWS.Lambda = new AWS.Lambda({ region });
const lambdas = await listLambdaFunctions(lambda, nextMarker);
let i = 0;
for (const fn of lambdas.Functions ?? []) {
if (fn.FunctionName) {
const versions = await listLambdaVersions(lambda, fn.FunctionName);
for (const version of versions.Versions ?? []) {
if (version.Version && version.Version !== fn.Version) {
if (removeIt) {
try {
console.log(
`Removing function: ${fn.FunctionName} - ${version.Version}`
);
await removeLambdaVersion(
lambda,
fn.FunctionName,
version.Version
);
i++;
} catch (e) {
console.log(
`Remove failed (${fn.FunctionName} - ${version.Version}): ${e.message}`
);
}
} else {
console.log(
`Listing function only: ${fn.FunctionName} - ${version.Version}`
);
i++;
}
}
}
}
}
if (lambdas.NextMarker) {
i += await cleanupVersions(region, removeIt, lambdas.NextMarker);
}
return i;
}
console.info("Cleaning up old Lambda versions");
cleanupVersions("us-west-2", true) // All Lambda@Edge is created in us-west-2 only
.then((success) => {
console.info(
`Cleaning up old Lambda versions successful. Count: ${success}`
);
process.exit(0);
})
.catch((error) => {
console.error(`Unhandled error: ${error}`);
process.exit(1);
});