Skip to content

Commit

Permalink
Merge pull request #264 from horike37/feature/conflict_with_pseudo_pa…
Browse files Browse the repository at this point in the history
…rams

Feature/conflict with pseudo params
  • Loading branch information
horike37 committed Oct 3, 2019
2 parents 1821b81 + 33c2afa commit c794fb4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/deploy/stepFunctions/compileStateMachines.js
Expand Up @@ -62,6 +62,25 @@ function* getIntrinsicFunctions(obj) {
}
}

// replace any pseudo parameters, e.g. #{AWS::Region} or #{AWS::AccountId}
function replacePseudoParameters(obj) {
const json = JSON.stringify(obj);

const regex = /#{([^}]+)}/g;
if (json.search(regex) >= 0) {
const newJson = json.replace(regex, '${$1}');
return {
replaced: true,
definition: JSON.parse(newJson),
};
}

return {
replaced: false,
definition: obj,
};
}

module.exports = {
compileStateMachines() {
if (this.isStateMachines()) {
Expand Down Expand Up @@ -98,13 +117,19 @@ module.exports = {
.replace(/\\n|\\r|\\n\\r/g, '');
} else {
const functionMappings = Array.from(getIntrinsicFunctions(stateMachineObj.definition));
const definitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
const { replaced, definition } = replacePseudoParameters(stateMachineObj.definition);
const definitionString = JSON.stringify(definition, undefined, 2);

if (_.isEmpty(functionMappings)) {
if (!replaced && _.isEmpty(functionMappings)) {
DefinitionString = definitionString;
} else if (_.isEmpty(functionMappings)) {
DefinitionString = {
'Fn::Sub': definitionString,
};
} else {
const f = translateLocalFunctionNames.bind(this);
const params = _.fromPairs(functionMappings.map(([k, v]) => [k, f(v)]));

DefinitionString = {
'Fn::Sub': [
definitionString,
Expand Down
42 changes: 41 additions & 1 deletion lib/deploy/stepFunctions/compileStateMachines.test.js
Expand Up @@ -336,7 +336,7 @@ describe('#compileStateMachines', () => {
States: {
HelloWorld: {
Type: 'Task',
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
Resource: 'arn:aws:lambda:us-east-1:1234567890:function:hello',
End: true,
},
},
Expand Down Expand Up @@ -1108,4 +1108,44 @@ describe('#compileStateMachines', () => {
// Definition is invalid and validate=true, should throw
expect(() => serverlessStepFunctions.compileStateMachines()).to.throw(Error);
});

it('should replace pseudo parameters that starts with #', () => {
const definition = {
StartAt: 'A',
States: {
A: {
Type: 'Task',
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
End: true,
},
},
};

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
name: 'stateMachineBeta1',
definition,
},
},
};

serverlessStepFunctions.compileStateMachines();
const actual = serverlessStepFunctions
.serverless
.service
.provider
.compiledCloudFormationTemplate
.Resources
.StateMachineBeta1
.Properties
.DefinitionString;

expect(actual).to.haveOwnProperty('Fn::Sub');
const definitionString = actual['Fn::Sub'];
expect(definitionString).to.contain('${AWS::Region}');
expect(definitionString).to.not.contain('#{AWS::Region}');
expect(definitionString).to.contain('${AWS::AccountId}');
expect(definitionString).to.not.contain('#{AWS::AccountId}');
});
});

0 comments on commit c794fb4

Please sign in to comment.