Skip to content

Commit 6a698b9

Browse files
committed
add all functions deployment
1 parent 656d8d5 commit 6a698b9

File tree

1 file changed

+119
-32
lines changed

1 file changed

+119
-32
lines changed

index.js

Lines changed: 119 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ServerlessStepFunctions {
1414
this.stage = this.provider.getStage();
1515
this.awsStateLanguage = {};
1616
this.functionArns = {};
17+
this.iamRoleArn = {};
1718
this.iamPolicyStatement = `{
1819
"Version": "2012-10-17",
1920
"Statement": [
@@ -54,7 +55,6 @@ class ServerlessStepFunctions {
5455
state: {
5556
usage: 'Name of the State Machine',
5657
shortcut: 't',
57-
required: true,
5858
},
5959
stage: {
6060
usage: 'Stage of the service',
@@ -186,15 +186,27 @@ class ServerlessStepFunctions {
186186
}
187187

188188
stateMachineDeploy() {
189-
this.serverless.cli.log(`Start to deploy ${this.options.state} step function...`);
190-
return BbPromise.bind(this)
191-
.then(this.yamlParse)
192-
.then(this.getStateMachineArn)
193-
.then(this.getFunctionArns)
194-
.then(this.compile)
195-
.then(this.getIamRole)
196-
.then(this.deleteStateMachine)
197-
.then(this.createStateMachine);
189+
if (this.options.state) {
190+
this.serverless.cli.log(`Start to deploy ${this.options.state} step function...`);
191+
return BbPromise.bind(this)
192+
.then(this.yamlParse)
193+
.then(this.getStateMachineArn)
194+
.then(this.getFunctionArns)
195+
.then(this.compile)
196+
.then(this.getIamRole)
197+
.then(this.deleteStateMachine)
198+
.then(this.createStateMachine);
199+
} else {
200+
this.serverless.cli.log(`Start to deploy all step functions...`);
201+
return BbPromise.bind(this)
202+
.then(this.yamlParse)
203+
.then(this.getStateMachineNames)
204+
.then(this.getFunctionArns)
205+
.then(this.compileAll)
206+
.then(this.getIamRoles)
207+
.then(this.deleteStateMachines)
208+
.then(this.createStateMachines);
209+
}
198210
}
199211

200212
stateMachineRemove() {
@@ -224,8 +236,8 @@ class ServerlessStepFunctions {
224236
// todo
225237
}
226238

227-
getIamRoleName() {
228-
let name = `${this.service}-${this.region}-${this.stage}-${this.options.state}-`;
239+
getIamRoleName(state) {
240+
let name = `${this.service}-${this.region}-${this.stage}-${state}-`;
229241
name += 'ssf-exerole';
230242
return name.substr(0, 64);
231243
}
@@ -236,29 +248,41 @@ class ServerlessStepFunctions {
236248
return name.substr(0, 64);
237249
}
238250

239-
getStateMachineName() {
240-
return `${this.service}-${this.stage}-${this.options.state}`;
251+
getStateMachineName(state) {
252+
return `${this.service}-${this.stage}-${state}`;
241253
}
242254

243-
getIamRole() {
255+
getIamRole(state) {
256+
state = state || this.options.state;
244257
return this.provider.request('IAM',
245258
'getRole',
246259
{
247-
RoleName: this.getIamRoleName(),
260+
RoleName: this.getIamRoleName(state),
248261
},
249262
this.options.stage,
250263
this.options.region)
251264
.then((result) => {
252-
this.iamRoleArn = result.Role.Arn;
265+
this.iamRoleArn[state] = result.Role.Arn;
253266
return BbPromise.resolve();
254267
}).catch((error) => {
255268
if (error.statusCode === 404) {
256-
return this.createIamRole();
269+
return this.createIamRole(state);
257270
}
258271
throw new this.serverless.classes.Error(error.message);
259272
});
260273
}
261274

275+
getIamRoles() {
276+
const promises = [];
277+
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
278+
promises.push(key);
279+
});
280+
281+
return BbPromise.map(promises, (value) => {
282+
return this.getIamRole(value);
283+
}).then(() => BbPromise.resolve());
284+
}
285+
262286
getFunctionArns() {
263287
return this.provider.request('STS',
264288
'getCallerIdentity',
@@ -274,17 +298,18 @@ class ServerlessStepFunctions {
274298
});
275299
}
276300

277-
createIamRole() {
301+
createIamRole(state) {
302+
state = state || this.options.state;
278303
return this.provider.request('IAM',
279304
'createRole',
280305
{
281306
AssumeRolePolicyDocument: this.assumeRolePolicyDocument,
282-
RoleName: this.getIamRoleName(),
307+
RoleName: this.getIamRoleName(state),
283308
},
284309
this.options.stage,
285310
this.options.region)
286311
.then((result) => {
287-
this.iamRoleArn = result.Role.Arn;
312+
this.iamRoleArn[state] = result.Role.Arn;
288313
return this.provider.request('IAM',
289314
'createPolicy',
290315
{
@@ -298,7 +323,7 @@ class ServerlessStepFunctions {
298323
'attachRolePolicy',
299324
{
300325
PolicyArn: result.Policy.Arn,
301-
RoleName: this.getIamRoleName(),
326+
RoleName: this.getIamRoleName(state),
302327
},
303328
this.options.stage,
304329
this.options.region)
@@ -320,7 +345,7 @@ class ServerlessStepFunctions {
320345
'detachRolePolicy',
321346
{
322347
PolicyArn: policyArn,
323-
RoleName: this.getIamRoleName(),
348+
RoleName: this.getIamRoleName(this.options.state),
324349
},
325350
this.options.stage,
326351
this.options.region);
@@ -336,7 +361,7 @@ class ServerlessStepFunctions {
336361
.then(() => this.provider.request('IAM',
337362
'deleteRole',
338363
{
339-
RoleName: this.getIamRoleName(),
364+
RoleName: this.getIamRoleName(this.options.state),
340365
},
341366
this.options.stage,
342367
this.options.region)
@@ -357,41 +382,81 @@ class ServerlessStepFunctions {
357382
});
358383
}
359384

360-
deleteStateMachine() {
385+
getStateMachineNames() {
386+
return this.provider.request('STS',
387+
'getCallerIdentity',
388+
{},
389+
this.options.stage,
390+
this.options.region)
391+
.then((result) => {
392+
this.stateMachineArns = {};
393+
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
394+
this.stateMachineArns[key] =
395+
`arn:aws:states:${this.region}:${result.Account}:stateMachine:${key}`;
396+
});
397+
return BbPromise.resolve();
398+
});
399+
}
400+
401+
deleteStateMachine(state) {
402+
state = state || this.options.state;
361403
return this.provider.request('StepFunctions',
362404
'deleteStateMachine',
363405
{
364-
stateMachineArn: this.stateMachineArn,
406+
stateMachineArn: this.stateMachineArns[state],
365407
},
366408
this.options.stage,
367409
this.options.region)
368410
.then(() => BbPromise.resolve());
369411
}
370412

371-
createStateMachine() {
413+
deleteStateMachines() {
414+
const promises = [];
415+
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
416+
promises.push(key);
417+
});
418+
419+
return BbPromise.map(promises, (state) => {
420+
return this.deleteStateMachine(state);
421+
}).then(() => BbPromise.resolve());
422+
}
423+
424+
createStateMachine(state) {
425+
state = state || this.options.state;
372426
return this.provider.request('StepFunctions',
373427
'createStateMachine',
374428
{
375-
definition: this.awsStateLanguage[this.options.state],
376-
name: this.getStateMachineName(),
377-
roleArn: this.iamRoleArn,
429+
definition: this.serverless.service.stepFunctions[state],
430+
name: this.getStateMachineName(state),
431+
roleArn: this.iamRoleArn[state],
378432
},
379433
this.options.stage,
380434
this.options.region)
381435
.then(() => {
382436
this.serverless.cli.consoleLog('');
383-
this.serverless.cli.log(`Finish to deploy ${this.getStateMachineName()} step function`);
437+
this.serverless.cli.log(`Finish to deploy ${this.getStateMachineName(state)} step function`);
384438
return BbPromise.resolve();
385439
}).catch((error) => {
386440
if (error.message.match(/State Machine is being deleted/)) {
387441
this.serverless.cli.printDot();
388-
setTimeout(this.createStateMachine.bind(this), 5000);
442+
setTimeout(this.createStateMachine(state).bind(this), 5000);
389443
} else {
390444
throw new this.serverless.classes.Error(error.message);
391445
}
392446
});
393447
}
394448

449+
createStateMachines() {
450+
const promises = [];
451+
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
452+
promises.push(key);
453+
});
454+
455+
return BbPromise.map(promises, (state) => {
456+
return this.createStateMachine(state);
457+
}).then(() => BbPromise.resolve());
458+
}
459+
395460
parseInputdate() {
396461
if (!this.options.data && this.options.path) {
397462
const absolutePath = path.isAbsolute(this.options.path) ?
@@ -512,5 +577,27 @@ class ServerlessStepFunctions {
512577
});
513578
return BbPromise.resolve();
514579
}
580+
581+
compileAll() {
582+
if (!this.serverless.service.stepFunctions) {
583+
const errorMessage = [
584+
'stepFunctions statement does not exists in serverless.yml',
585+
].join('');
586+
throw new this.serverless.classes.Error(errorMessage);
587+
}
588+
589+
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
590+
this.serverless.service.stepFunctions[stepFunctionKey] = JSON.stringify(stepFunctionObj);
591+
});
592+
593+
_.forEach(this.functionArns, (functionObj, functionKey) => {
594+
const regExp = new RegExp(`"Resource":"${functionKey}"`, 'g');
595+
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
596+
this.serverless.service.stepFunctions[stepFunctionKey] =
597+
this.serverless.service.stepFunctions[stepFunctionKey].replace(regExp, `"Resource":"${functionObj}"`);
598+
});
599+
});
600+
return BbPromise.resolve();
601+
}
515602
}
516603
module.exports = ServerlessStepFunctions;

0 commit comments

Comments
 (0)