Skip to content

Commit 57e1723

Browse files
feat: support custom state machine names when using a distributed mapA
1 parent fcf2b3c commit 57e1723

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
@@ -597,14 +597,15 @@ module.exports = {
597597
logger.config(this.serverless, this.v3Api);
598598
const service = this.serverless.service;
599599
const permissionsBoundary = service.provider.rolePermissionsBoundary;
600-
this.getAllStateMachines().forEach((stateMachineName) => {
601-
const stateMachineObj = this.getStateMachine(stateMachineName);
600+
this.getAllStateMachines().forEach((stateMachineId) => {
601+
const stateMachineObj = this.getStateMachine(stateMachineId);
602+
const stateMachineName = stateMachineObj.name || stateMachineId;
602603
if (stateMachineObj.role) {
603604
return;
604605
}
605606

606607
if (!stateMachineObj.definition) {
607-
throw new Error(`Missing "definition" for state machine ${stateMachineName}`);
608+
throw new Error(`Missing "definition" for state machine ${stateMachineId}`);
608609
}
609610

610611
const taskStates = getTaskStates(stateMachineObj.definition.States, stateMachineName);
@@ -646,7 +647,7 @@ module.exports = {
646647
}
647648

648649
const stateMachineLogicalId = this.getStateMachineLogicalId(
649-
stateMachineName,
650+
stateMachineId,
650651
stateMachineObj,
651652
);
652653
const iamRoleStateMachineLogicalId = `${stateMachineLogicalId}Role`;

lib/deploy/stepFunctions/compileIamRole.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,57 @@ describe('#compileIamRole', () => {
23442344
]);
23452345
});
23462346

2347+
it('should support custom state machine names in a Distributed Map', () => {
2348+
const getStateMachine = (id, lambdaArn) => ({
2349+
id,
2350+
name: 'DistributedMapper',
2351+
definition: {
2352+
StartAt: 'A',
2353+
States: {
2354+
A: {
2355+
Type: 'Map',
2356+
ItemProcessor: {
2357+
ProcessorConfig: {
2358+
Mode: 'DISTRIBUTED',
2359+
},
2360+
StartAt: 'B',
2361+
States: {
2362+
B: {
2363+
Type: 'Task',
2364+
Resource: lambdaArn,
2365+
End: true,
2366+
},
2367+
},
2368+
},
2369+
End: true,
2370+
},
2371+
},
2372+
},
2373+
});
2374+
2375+
serverless.service.stepFunctions = {
2376+
stateMachines: {
2377+
myStateMachine: getStateMachine('StateMachine1', 'arn:aws:lambda:us-west-2:1234567890:function:foo'),
2378+
},
2379+
};
2380+
2381+
serverlessStepFunctions.compileIamRole();
2382+
2383+
const statements = serverlessStepFunctions.serverless.service
2384+
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
2385+
.Properties.Policies[0].PolicyDocument.Statement;
2386+
2387+
const stepFunctionPermission = statements.filter(s => _.isEqual(s.Action, ['states:StartExecution']));
2388+
expect(stepFunctionPermission).to.have.lengthOf(1);
2389+
expect(stepFunctionPermission[0].Resource).to.deep.eq([{
2390+
'Fn::Sub': [
2391+
'arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:DistributedMapper',
2392+
{},
2393+
],
2394+
},
2395+
]);
2396+
});
2397+
23472398
it('should support nested Map state type', () => {
23482399
const getStateMachine = (id, lambdaArn1, lambdaArn2) => ({
23492400
id,

0 commit comments

Comments
 (0)