Skip to content

Commit c794fb4

Browse files
authored
Merge pull request #264 from horike37/feature/conflict_with_pseudo_params
Feature/conflict with pseudo params
2 parents 1821b81 + 33c2afa commit c794fb4

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ function* getIntrinsicFunctions(obj) {
6262
}
6363
}
6464

65+
// replace any pseudo parameters, e.g. #{AWS::Region} or #{AWS::AccountId}
66+
function replacePseudoParameters(obj) {
67+
const json = JSON.stringify(obj);
68+
69+
const regex = /#{([^}]+)}/g;
70+
if (json.search(regex) >= 0) {
71+
const newJson = json.replace(regex, '${$1}');
72+
return {
73+
replaced: true,
74+
definition: JSON.parse(newJson),
75+
};
76+
}
77+
78+
return {
79+
replaced: false,
80+
definition: obj,
81+
};
82+
}
83+
6584
module.exports = {
6685
compileStateMachines() {
6786
if (this.isStateMachines()) {
@@ -98,13 +117,19 @@ module.exports = {
98117
.replace(/\\n|\\r|\\n\\r/g, '');
99118
} else {
100119
const functionMappings = Array.from(getIntrinsicFunctions(stateMachineObj.definition));
101-
const definitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
120+
const { replaced, definition } = replacePseudoParameters(stateMachineObj.definition);
121+
const definitionString = JSON.stringify(definition, undefined, 2);
102122

103-
if (_.isEmpty(functionMappings)) {
123+
if (!replaced && _.isEmpty(functionMappings)) {
104124
DefinitionString = definitionString;
125+
} else if (_.isEmpty(functionMappings)) {
126+
DefinitionString = {
127+
'Fn::Sub': definitionString,
128+
};
105129
} else {
106130
const f = translateLocalFunctionNames.bind(this);
107131
const params = _.fromPairs(functionMappings.map(([k, v]) => [k, f(v)]));
132+
108133
DefinitionString = {
109134
'Fn::Sub': [
110135
definitionString,

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe('#compileStateMachines', () => {
336336
States: {
337337
HelloWorld: {
338338
Type: 'Task',
339-
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
339+
Resource: 'arn:aws:lambda:us-east-1:1234567890:function:hello',
340340
End: true,
341341
},
342342
},
@@ -1108,4 +1108,44 @@ describe('#compileStateMachines', () => {
11081108
// Definition is invalid and validate=true, should throw
11091109
expect(() => serverlessStepFunctions.compileStateMachines()).to.throw(Error);
11101110
});
1111+
1112+
it('should replace pseudo parameters that starts with #', () => {
1113+
const definition = {
1114+
StartAt: 'A',
1115+
States: {
1116+
A: {
1117+
Type: 'Task',
1118+
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
1119+
End: true,
1120+
},
1121+
},
1122+
};
1123+
1124+
serverless.service.stepFunctions = {
1125+
stateMachines: {
1126+
myStateMachine1: {
1127+
name: 'stateMachineBeta1',
1128+
definition,
1129+
},
1130+
},
1131+
};
1132+
1133+
serverlessStepFunctions.compileStateMachines();
1134+
const actual = serverlessStepFunctions
1135+
.serverless
1136+
.service
1137+
.provider
1138+
.compiledCloudFormationTemplate
1139+
.Resources
1140+
.StateMachineBeta1
1141+
.Properties
1142+
.DefinitionString;
1143+
1144+
expect(actual).to.haveOwnProperty('Fn::Sub');
1145+
const definitionString = actual['Fn::Sub'];
1146+
expect(definitionString).to.contain('${AWS::Region}');
1147+
expect(definitionString).to.not.contain('#{AWS::Region}');
1148+
expect(definitionString).to.contain('${AWS::AccountId}');
1149+
expect(definitionString).to.not.contain('#{AWS::AccountId}');
1150+
});
11111151
});

0 commit comments

Comments
 (0)