-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldLambdaVersions.mjs
76 lines (63 loc) · 1.74 KB
/
oldLambdaVersions.mjs
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
import {
DeleteFunctionCommand,
LambdaClient,
ListAliasesCommand,
ListFunctionsCommand,
paginateListVersionsByFunction,
} from "@aws-sdk/client-lambda";
import { fromSSO } from "@aws-sdk/credential-providers";
//Delete all but 'keepversions' highest numbered numeric versions
const keepversions = 3;
const awsRegion = "us-west-2";
const client = new LambdaClient({
region: awsRegion,
credentials: fromSSO({ profile: "stage" }),
});
async function main() {
const fList = await client.send(
new ListFunctionsCommand({ region: awsRegion }),
);
for (let func of fList.Functions) {
let resVer = [];
for await (const data of paginateListVersionsByFunction(
{ client },
{ FunctionName: func.FunctionName },
)) {
resVer.push(...(data.Versions ?? []));
}
let verList = new Set();
for (const row of resVer) {
if (row.Version != "$LATEST" && !Number.isNaN(row.Version))
verList.add(Number(row.Version));
}
let resAlias = await client.send(
new ListAliasesCommand({ FunctionName: func.FunctionArn }),
);
for (const row of resAlias.Aliases) {
if (
row.FunctionVersion != "$LATEST" &&
!Number.isNaN(row.FunctionVersion)
) {
verList.delete(Number(row.FunctionVersion));
}
}
let remVerList = Array.from(
new Int32Array(verList.values()).sort().reverse(),
);
remVerList.splice(0, keepversions);
for (const row of remVerList) {
const arn = `${func.FunctionArn}:${row}`;
console.log("removing:", arn);
await client.send(
new DeleteFunctionCommand({
FunctionName: arn,
}),
);
}
}
}
main()
.then(() => process.exit(0))
.catch((e) => {
console.error(e);
});