Skip to content

Commit 47fcb4e

Browse files
authored
Merge pull request serverless-operations#552 from toddhainsworth/feature/distributed-map-custom-name
feat: support custom state machine names when using a distributed mapA
2 parents 7982482 + 57e1723 commit 47fcb4e

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

lib/deploy/stepFunctions/compileIamRole.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,15 @@ module.exports = {
614614
logger.config(this.serverless, this.v3Api);
615615
const service = this.serverless.service;
616616
const permissionsBoundary = service.provider.rolePermissionsBoundary;
617-
this.getAllStateMachines().forEach((stateMachineName) => {
618-
const stateMachineObj = this.getStateMachine(stateMachineName);
617+
this.getAllStateMachines().forEach((stateMachineId) => {
618+
const stateMachineObj = this.getStateMachine(stateMachineId);
619+
const stateMachineName = stateMachineObj.name || stateMachineId;
619620
if (stateMachineObj.role) {
620621
return;
621622
}
622623

623624
if (!stateMachineObj.definition) {
624-
throw new Error(`Missing "definition" for state machine ${stateMachineName}`);
625+
throw new Error(`Missing "definition" for state machine ${stateMachineId}`);
625626
}
626627

627628
const taskStates = getTaskStates(stateMachineObj.definition.States, stateMachineName);
@@ -663,7 +664,7 @@ module.exports = {
663664
}
664665

665666
const stateMachineLogicalId = this.getStateMachineLogicalId(
666-
stateMachineName,
667+
stateMachineId,
667668
stateMachineObj,
668669
);
669670
const iamRoleStateMachineLogicalId = `${stateMachineLogicalId}Role`;

lib/deploy/stepFunctions/compileIamRole.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,57 @@ describe('#compileIamRole', () => {
24742474
]);
24752475
});
24762476

2477+
it('should support custom state machine names in a Distributed Map', () => {
2478+
const getStateMachine = (id, lambdaArn) => ({
2479+
id,
2480+
name: 'DistributedMapper',
2481+
definition: {
2482+
StartAt: 'A',
2483+
States: {
2484+
A: {
2485+
Type: 'Map',
2486+
ItemProcessor: {
2487+
ProcessorConfig: {
2488+
Mode: 'DISTRIBUTED',
2489+
},
2490+
StartAt: 'B',
2491+
States: {
2492+
B: {
2493+
Type: 'Task',
2494+
Resource: lambdaArn,
2495+
End: true,
2496+
},
2497+
},
2498+
},
2499+
End: true,
2500+
},
2501+
},
2502+
},
2503+
});
2504+
2505+
serverless.service.stepFunctions = {
2506+
stateMachines: {
2507+
myStateMachine: getStateMachine('StateMachine1', 'arn:aws:lambda:us-west-2:1234567890:function:foo'),
2508+
},
2509+
};
2510+
2511+
serverlessStepFunctions.compileIamRole();
2512+
2513+
const statements = serverlessStepFunctions.serverless.service
2514+
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
2515+
.Properties.Policies[0].PolicyDocument.Statement;
2516+
2517+
const stepFunctionPermission = statements.filter(s => _.isEqual(s.Action, ['states:StartExecution']));
2518+
expect(stepFunctionPermission).to.have.lengthOf(1);
2519+
expect(stepFunctionPermission[0].Resource).to.deep.eq([{
2520+
'Fn::Sub': [
2521+
'arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:DistributedMapper',
2522+
{},
2523+
],
2524+
},
2525+
]);
2526+
});
2527+
24772528
it('should support nested Map state type', () => {
24782529
const getStateMachine = (id, lambdaArn1, lambdaArn2) => ({
24792530
id,

0 commit comments

Comments
 (0)