forked from ljacobsson/lambda-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.js
80 lines (65 loc) · 3.06 KB
/
cleanup.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
import fs from 'fs';
import { GetTemplateCommand, CloudFormationClient, ListStackResourcesCommand } from "@aws-sdk/client-cloudformation"
import { LambdaClient, UpdateFunctionCodeCommand, UpdateFunctionConfigurationCommand } from "@aws-sdk/client-lambda"
import ini from 'ini';
import { fromSSO } from '@aws-sdk/credential-provider-sso';
console.log("Cleaning up....");
let configEnv = 'default';
let functions = undefined;
const cachePath = process.cwd() + "/.lambda-debug";
if (fs.existsSync(cachePath)) {
const conf = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
configEnv = conf.configEnv || 'default';
if (conf.functions) {
functions = conf.functions;
}
fs.unlinkSync(process.cwd() + "/.lambda-debug");
}
const config = ini.parse(fs.readFileSync(process.cwd() + "/samconfig.toml", 'utf-8'));
const samConfig = config[configEnv].deploy.parameters;
const stackName = samConfig.stack_name || config[configEnv].global.parameters.stack_name;
const profile = samConfig.profile || "default";
const cfnClient = new CloudFormationClient({ credentials: fromSSO({ profile }) });
const lambdaClient = new LambdaClient({ credentials: fromSSO({ profile }) });
const templateResponse = await cfnClient.send(new GetTemplateCommand({ StackName: stackName, TemplateStage: "Processed" }));
const stack = await cfnClient.send(new ListStackResourcesCommand({ StackName: stackName }));
const template = JSON.parse(templateResponse.TemplateBody);
functions = functions || Object.keys(template.Resources).filter(key => template.Resources[key].Type === "AWS::Lambda::Function");;
const updatePromises = functions.map(async functionName => {
let updated = false;
do {
try {
const func = template.Resources[functionName];
const physicalId = stack.StackResourceSummaries.find(resource => resource.LogicalResourceId === functionName).PhysicalResourceId;
console.log(`Restoring function: ${functionName}`);
await lambdaClient.send(new UpdateFunctionConfigurationCommand({
FunctionName: physicalId,
Timeout: func.Properties.Timeout,
MemorySize: func.Properties.MemorySize,
Handler: func.Properties.Handler,
}));
// Sleep 1 second to avoid throttling
await new Promise(resolve => setTimeout(resolve, 1000));
await lambdaClient.send(new UpdateFunctionCodeCommand({
FunctionName: physicalId,
Publish: true,
S3Bucket: func.Properties.Code.S3Bucket,
S3Key: func.Properties.Code.S3Key,
}));
console.log("Restored function:", functionName);
updated = true;
} catch (error) {
if (error.name === "TooManyRequestsException") {
console.log("Too many requests, sleeping for 1 second");
await new Promise(resolve => setTimeout(resolve, 1000));
} else if (error.name === "ResourceConflictException") {
console.log("Resource conflict, retrying");
await new Promise(resolve => setTimeout(resolve, 1000));
} else {
throw error;
}
}
} while (!updated);
});
// Wait for all promises to resolve
await Promise.all(updatePromises);