Skip to content

Commit

Permalink
Merge pull request #552 from toddhainsworth/feature/distributed-map-c…
Browse files Browse the repository at this point in the history
…ustom-name

feat: support custom state machine names when using a distributed mapA
  • Loading branch information
lopburny authored Mar 8, 2023
2 parents 7982482 + 57e1723 commit 47fcb4e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,14 +614,15 @@ module.exports = {
logger.config(this.serverless, this.v3Api);
const service = this.serverless.service;
const permissionsBoundary = service.provider.rolePermissionsBoundary;
this.getAllStateMachines().forEach((stateMachineName) => {
const stateMachineObj = this.getStateMachine(stateMachineName);
this.getAllStateMachines().forEach((stateMachineId) => {
const stateMachineObj = this.getStateMachine(stateMachineId);
const stateMachineName = stateMachineObj.name || stateMachineId;
if (stateMachineObj.role) {
return;
}

if (!stateMachineObj.definition) {
throw new Error(`Missing "definition" for state machine ${stateMachineName}`);
throw new Error(`Missing "definition" for state machine ${stateMachineId}`);
}

const taskStates = getTaskStates(stateMachineObj.definition.States, stateMachineName);
Expand Down Expand Up @@ -663,7 +664,7 @@ module.exports = {
}

const stateMachineLogicalId = this.getStateMachineLogicalId(
stateMachineName,
stateMachineId,
stateMachineObj,
);
const iamRoleStateMachineLogicalId = `${stateMachineLogicalId}Role`;
Expand Down
51 changes: 51 additions & 0 deletions lib/deploy/stepFunctions/compileIamRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,57 @@ describe('#compileIamRole', () => {
]);
});

it('should support custom state machine names in a Distributed Map', () => {
const getStateMachine = (id, lambdaArn) => ({
id,
name: 'DistributedMapper',
definition: {
StartAt: 'A',
States: {
A: {
Type: 'Map',
ItemProcessor: {
ProcessorConfig: {
Mode: 'DISTRIBUTED',
},
StartAt: 'B',
States: {
B: {
Type: 'Task',
Resource: lambdaArn,
End: true,
},
},
},
End: true,
},
},
},
});

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine: getStateMachine('StateMachine1', 'arn:aws:lambda:us-west-2:1234567890:function:foo'),
},
};

serverlessStepFunctions.compileIamRole();

const statements = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
.Properties.Policies[0].PolicyDocument.Statement;

const stepFunctionPermission = statements.filter(s => _.isEqual(s.Action, ['states:StartExecution']));
expect(stepFunctionPermission).to.have.lengthOf(1);
expect(stepFunctionPermission[0].Resource).to.deep.eq([{
'Fn::Sub': [
'arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:DistributedMapper',
{},
],
},
]);
});

it('should support nested Map state type', () => {
const getStateMachine = (id, lambdaArn1, lambdaArn2) => ({
id,
Expand Down

0 comments on commit 47fcb4e

Please sign in to comment.