Skip to content

Custom naming for serverless-step-functions #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,26 @@ plugins:
- serverless-step-functions
```

### Adding a custom name for a stateMachine
In case you need to interpolate a specific stage or service layer variable as the
stateMachines name you can add a `Name` property to your yaml.

```yml
service: messager

functions:
sendMessage:
handler: handler.sendMessage

stepFunctions:
stateMachines:
sendMessageFunc:
Name: sendMessageFunc-${self:custom.service}-${opt:stage}
definition:
<your definition>

plugins:
- serverless-step-functions
```

Please note, that during normalization some characters will be changed to adhere to CloudFormation templates.
11 changes: 9 additions & 2 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module.exports = {
let DefinitionString;
let RoleArn;
let DependsOn;
let Name;

if (stateMachineObj.Name) {
Name = stateMachineObj.Name;
}

if (stateMachineObj.definition) {
DefinitionString = JSON.stringify(stateMachineObj.definition);
Expand Down Expand Up @@ -47,8 +52,9 @@ module.exports = {
DependsOn = 'IamRoleStateMachineExecution';
}

const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName);
const stateMachineOutputLogicalId = this.getStateMachineOutputLogicalId(stateMachineName);
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, Name);
const stateMachineOutputLogicalId = this
.getStateMachineOutputLogicalId(stateMachineName, Name);

const stateMachineTemplate = `
{
Expand Down Expand Up @@ -83,6 +89,7 @@ module.exports = {
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs,
newStateMachineOutPutObject
);

return BbPromise.resolve();
});
}
Expand Down
49 changes: 49 additions & 0 deletions lib/deploy/stepFunctions/compileStateMachines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,55 @@ describe('#compileStateMachines', () => {
).to.equal('MyStateMachine2StepFunctionsStateMachine');
});

it('should create named resources when Name is provided', () => {
serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
definition: 'definition1',
Name: 'stateMachineBeta1',
},
myStateMachine2: {
definition: 'definition2',
Name: 'stateMachineBeta2',
},
},
};

serverlessStepFunctions.compileStateMachines();
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1.Type
).to.equal('AWS::StepFunctions::StateMachine');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta2.Type
).to.equal('AWS::StepFunctions::StateMachine');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1.Properties.DefinitionString
).to.equal('"definition1"');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta2.Properties.DefinitionString
).to.equal('"definition2"');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1.Properties.RoleArn['Fn::GetAtt'][0]
).to.equal('IamRoleStateMachineExecution');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta2.Properties.RoleArn['Fn::GetAtt'][0]
).to.equal('IamRoleStateMachineExecution');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1.DependsOn
).to.equal('IamRoleStateMachineExecution');
expect(serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta2.DependsOn
).to.equal('IamRoleStateMachineExecution');
});

it('should create corresponding resources when definition and role property are given', () => {
serverless.service.stepFunctions = {
stateMachines: {
Expand Down
10 changes: 8 additions & 2 deletions lib/naming.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';

module.exports = {
getStateMachineLogicalId(stateMachineName) {
getStateMachineLogicalId(stateMachineName, Name) {
if (Name) {
return `${this.provider.naming.getNormalizedFunctionName(Name)}`;
}
return `${this.provider.naming
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachine`;
},

getStateMachineOutputLogicalId(stateMachineName) {
getStateMachineOutputLogicalId(stateMachineName, Name) {
if (Name) {
return `${this.provider.naming.getNormalizedFunctionName(Name)}Arn`;
}
return `${this.provider.naming
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachineArn`;
},
Expand Down
14 changes: 14 additions & 0 deletions lib/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe('#naming', () => {
});
});

describe('#getStateMachineLogicalId() -- Named', () => {
it('should normalize the stateMachine name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine', 'alphaNumeric')).to
.equal('AlphaNumeric');
});
});

describe('#getStateMachineOutputLogicalId() -- Named', () => {
it('should normalize the stateMachine output name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine', 'alphaNumeric'))
.to.equal('AlphaNumericArn');
});
});

describe('#getActivityLogicalId()', () => {
it('should normalize the activity name and add the standard suffix', () => {
expect(serverlessStepFunctions.getActivityLogicalId('activity')).to
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-step-functions",
"version": "1.0.2",
"version": "1.0.3",
"description": "The module is AWS Step Functions plugin for Serverless Framework",
"main": "lib/index.js",
"scripts": {
Expand Down