-
Notifications
You must be signed in to change notification settings - Fork 21
/
powertune-lambda.js
261 lines (239 loc) · 6.88 KB
/
powertune-lambda.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const _ = require("lodash");
const { getAWSSDK } = require("../lib/aws");
const { getLatestVersion, deploy } = require("../lib/sar");
const { startStateMachine, waitForStateMachineOutput } = require("../lib/step-functions");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const fs = require("fs");
const inquirer = require("inquirer");
const childProcess = require("child_process");
const { track } = require("../lib/analytics");
require("colors");
const ApplicationId =
"arn:aws:serverlessrepo:us-east-1:451282441545:applications/aws-lambda-power-tuning";
const StackName = "serverlessrepo-lumigo-cli-powertuning-lambda";
class PowertuneLambdaCommand extends Command {
async run() {
const { flags } = this.parse(PowertuneLambdaCommand);
const {
functionName,
region,
profile,
strategy,
invocations,
file,
balancedWeight,
powerValues,
outputFile,
noVisualization,
autoOptimize,
autoOptimizeAlias
} = flags;
global.region = region;
global.profile = profile;
checkVersion();
track("powertune-lambda", { region, strategy });
this.log(`checking the aws-lambda-power-tuning SAR in [${region}]`);
const version = await getLatestVersion(ApplicationId);
this.log(`the latest version of aws-lambda-power-tuning SAR is ${version}`);
this.log(
`looking for deployed CloudFormation stack [${StackName}] in [${region}]`
);
let stateMachineArn;
const findCfnResult = await findCloudFormation(version);
switch (findCfnResult.result) {
case "not found":
this.log("stack is not found");
this.log(
`deploying the aws-lambda-power-tuning SAR [${version}] to [${region}]`
);
stateMachineArn = (await deploy(ApplicationId, version, StackName))
.StateMachineARN;
break;
case "outdated":
this.log(
`stack is deployed but is running an outdated version [${findCfnResult.version}]`
);
stateMachineArn = (await deploy(ApplicationId, version, StackName, true))
.StateMachineARN;
break;
default:
this.log("stack is deployed and up-to-date");
stateMachineArn = findCfnResult.stateMachineArn;
}
this.log(`the State Machine is ${stateMachineArn}`);
let payload = flags.payload || "{}";
if (file) {
this.log(`loading payload from [${file}]...`);
payload = fs.readFileSync(file, "utf8");
}
// eslint-disable-next-line no-unused-vars
const [_arn, _aws, _states, _region, accountId, ...rest] = stateMachineArn.split(
":"
);
const lambdaArn = `arn:aws:lambda:${region}:${accountId}:function:${functionName}`;
const input = JSON.stringify({
lambdaARN: lambdaArn,
num: invocations,
payload: payload,
parallelInvocation: false,
strategy,
balancedWeight,
powerValues,
autoOptimize,
autoOptimizeAlias
});
const executionArn = await startStateMachine(stateMachineArn, input);
this.log("State Machine execution started");
this.log(`execution ARN is ${executionArn}`);
const result = await waitForStateMachineOutput(executionArn);
result.functionName = functionName;
this.log(JSON.stringify(result, null, 2).yellow);
if (outputFile) {
fs.writeFileSync(outputFile, JSON.stringify(result, null, 2));
}
if (!noVisualization) {
// since v2.1.1 the powertuning SFN returns a visualization URL as well
const visualizationUrl = _.get(result, "stateMachine.visualization");
if (visualizationUrl) {
const { visualize } = await inquirer.prompt([
{
type: "list",
name: "visualize",
message:
"Do you want to open the visualization to see more results?",
choices: ["yes", "no"]
}
]);
if (visualize === "yes") {
openVisualization(visualizationUrl);
}
}
}
}
}
PowertuneLambdaCommand.description = "Powertunes a Lambda function for cost or speed";
PowertuneLambdaCommand.flags = {
functionName: flags.string({
char: "n",
description: "name of the Lambda function",
required: true
}),
region: flags.string({
char: "r",
description: "AWS region, e.g. us-east-1",
required: true
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
}),
strategy: flags.string({
char: "s",
description: 'what to powertune the function for - "cost", "speed" or "balanced"',
required: true,
options: ["cost", "speed", "balanced"]
}),
invocations: flags.integer({
char: "i",
description: "the number of invocations to run for each configuration",
required: false,
default: 100
}),
payload: flags.string({
char: "e",
description: "the JSON payload to send to the function",
required: false,
default: "{}"
}),
file: flags.string({
char: "f",
description: "file that contains the JSON payload to send to the function",
required: false,
exclusive: ["payload"]
}),
balancedWeight: flags.string({
char: "w",
description:
'the trade-off between cost and time, 0.0 is equivalent to "speed" strategy, 1.0 is equivalent to "cost" strategy',
required: false,
parse: x => parseFloat(x)
}),
powerValues: flags.string({
char: "v",
description:
"comma-separated list of power values to be tested, e.g. 128,256,512,1024",
required: false,
parse: x => {
if (x === "ALL") {
return "ALL";
} else {
return x.split(",").map(n => parseInt(n));
}
}
}),
outputFile: flags.string({
char: "o",
description: "output file for the powertune SAR response",
required: false
}),
noVisualization: flags.boolean({
char: "z",
description: "suppresses the prompt to show visualization",
default: false,
required: false
}),
autoOptimize: flags.boolean({
description:
"if true, apply the optimal configuration at the end of its execution",
default: false,
required: false
}),
autoOptimizeAlias: flags.string({
description:
"the state machine will create or update this alias with the new optimal power value",
dependsOn: ["autoOptimize"],
required: false
})
};
const openVisualization = url => {
try {
// this works on many platforms
childProcess.execSync(`python -m webbrowser "${url}"`);
} catch (err) {
childProcess.execSync(`open "${url}"`);
}
};
const findCloudFormation = async version => {
const AWS = getAWSSDK();
const CloudFormation = new AWS.CloudFormation();
try {
const resp = await CloudFormation.describeStacks({
StackName: StackName
}).promise();
const stack = resp.Stacks[0];
const semverTag = stack.Tags.find(
x => x.Key === "serverlessrepo:semanticVersion"
);
const currentVersion = semverTag.Value;
if (currentVersion !== version) {
return {
result: "outdated",
version: currentVersion
};
}
const smArnOutput = stack.Outputs.find(x => x.OutputKey === "StateMachineARN");
const stateMachineArn = smArnOutput.OutputValue;
return {
result: "active",
version: version,
stateMachineArn: stateMachineArn
};
} catch (err) {
return {
result: "not found"
};
}
};
module.exports = PowertuneLambdaCommand;