Skip to content

Commit 8b94d46

Browse files
authored
Merge pull request #50 from icarus-sullivan/CustomNaming
Custom naming for serverless-step-functions
2 parents 076e8e6 + 18aba0f commit 8b94d46

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,26 @@ plugins:
154154
- serverless-step-functions
155155
```
156156
157+
### Adding a custom name for a stateMachine
158+
In case you need to interpolate a specific stage or service layer variable as the
159+
stateMachines name you can add a `Name` property to your yaml.
160+
161+
```yml
162+
service: messager
163+
164+
functions:
165+
sendMessage:
166+
handler: handler.sendMessage
167+
168+
stepFunctions:
169+
stateMachines:
170+
sendMessageFunc:
171+
Name: sendMessageFunc-${self:custom.service}-${opt:stage}
172+
definition:
173+
<your definition>
174+
175+
plugins:
176+
- serverless-step-functions
177+
```
178+
179+
Please note, that during normalization some characters will be changed to adhere to CloudFormation templates.

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ module.exports = {
1010
let DefinitionString;
1111
let RoleArn;
1212
let DependsOn;
13+
let Name;
14+
15+
if (stateMachineObj.Name) {
16+
Name = stateMachineObj.Name;
17+
}
1318

1419
if (stateMachineObj.definition) {
1520
DefinitionString = JSON.stringify(stateMachineObj.definition);
@@ -47,8 +52,9 @@ module.exports = {
4752
DependsOn = 'IamRoleStateMachineExecution';
4853
}
4954

50-
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName);
51-
const stateMachineOutputLogicalId = this.getStateMachineOutputLogicalId(stateMachineName);
55+
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, Name);
56+
const stateMachineOutputLogicalId = this
57+
.getStateMachineOutputLogicalId(stateMachineName, Name);
5258

5359
const stateMachineTemplate = `
5460
{
@@ -83,6 +89,7 @@ module.exports = {
8389
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs,
8490
newStateMachineOutPutObject
8591
);
92+
8693
return BbPromise.resolve();
8794
});
8895
}

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,55 @@ describe('#compileStateMachines', () => {
7676
).to.equal('MyStateMachine2StepFunctionsStateMachine');
7777
});
7878

79+
it('should create named resources when Name is provided', () => {
80+
serverless.service.stepFunctions = {
81+
stateMachines: {
82+
myStateMachine1: {
83+
definition: 'definition1',
84+
Name: 'stateMachineBeta1',
85+
},
86+
myStateMachine2: {
87+
definition: 'definition2',
88+
Name: 'stateMachineBeta2',
89+
},
90+
},
91+
};
92+
93+
serverlessStepFunctions.compileStateMachines();
94+
expect(serverlessStepFunctions.serverless.service
95+
.provider.compiledCloudFormationTemplate.Resources
96+
.StateMachineBeta1.Type
97+
).to.equal('AWS::StepFunctions::StateMachine');
98+
expect(serverlessStepFunctions.serverless.service
99+
.provider.compiledCloudFormationTemplate.Resources
100+
.StateMachineBeta2.Type
101+
).to.equal('AWS::StepFunctions::StateMachine');
102+
expect(serverlessStepFunctions.serverless.service
103+
.provider.compiledCloudFormationTemplate.Resources
104+
.StateMachineBeta1.Properties.DefinitionString
105+
).to.equal('"definition1"');
106+
expect(serverlessStepFunctions.serverless.service
107+
.provider.compiledCloudFormationTemplate.Resources
108+
.StateMachineBeta2.Properties.DefinitionString
109+
).to.equal('"definition2"');
110+
expect(serverlessStepFunctions.serverless.service
111+
.provider.compiledCloudFormationTemplate.Resources
112+
.StateMachineBeta1.Properties.RoleArn['Fn::GetAtt'][0]
113+
).to.equal('IamRoleStateMachineExecution');
114+
expect(serverlessStepFunctions.serverless.service
115+
.provider.compiledCloudFormationTemplate.Resources
116+
.StateMachineBeta2.Properties.RoleArn['Fn::GetAtt'][0]
117+
).to.equal('IamRoleStateMachineExecution');
118+
expect(serverlessStepFunctions.serverless.service
119+
.provider.compiledCloudFormationTemplate.Resources
120+
.StateMachineBeta1.DependsOn
121+
).to.equal('IamRoleStateMachineExecution');
122+
expect(serverlessStepFunctions.serverless.service
123+
.provider.compiledCloudFormationTemplate.Resources
124+
.StateMachineBeta2.DependsOn
125+
).to.equal('IamRoleStateMachineExecution');
126+
});
127+
79128
it('should create corresponding resources when definition and role property are given', () => {
80129
serverless.service.stepFunctions = {
81130
stateMachines: {

lib/naming.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
'use strict';
22

33
module.exports = {
4-
getStateMachineLogicalId(stateMachineName) {
4+
getStateMachineLogicalId(stateMachineName, Name) {
5+
if (Name) {
6+
return `${this.provider.naming.getNormalizedFunctionName(Name)}`;
7+
}
58
return `${this.provider.naming
69
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachine`;
710
},
811

9-
getStateMachineOutputLogicalId(stateMachineName) {
12+
getStateMachineOutputLogicalId(stateMachineName, Name) {
13+
if (Name) {
14+
return `${this.provider.naming.getNormalizedFunctionName(Name)}Arn`;
15+
}
1016
return `${this.provider.naming
1117
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachineArn`;
1218
},

lib/naming.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe('#naming', () => {
3232
});
3333
});
3434

35+
describe('#getStateMachineLogicalId() -- Named', () => {
36+
it('should normalize the stateMachine name and add the standard suffix', () => {
37+
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine', 'alphaNumeric')).to
38+
.equal('AlphaNumeric');
39+
});
40+
});
41+
42+
describe('#getStateMachineOutputLogicalId() -- Named', () => {
43+
it('should normalize the stateMachine output name and add the standard suffix', () => {
44+
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine', 'alphaNumeric'))
45+
.to.equal('AlphaNumericArn');
46+
});
47+
});
48+
3549
describe('#getActivityLogicalId()', () => {
3650
it('should normalize the activity name and add the standard suffix', () => {
3751
expect(serverlessStepFunctions.getActivityLogicalId('activity')).to

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-step-functions",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "The module is AWS Step Functions plugin for Serverless Framework",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)