Skip to content

Commit 4256648

Browse files
authored
Merge pull request #258 from horike37/feature/mapstate
feat: support the new Map state
2 parents a8621c7 + 6f0d562 commit 4256648

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

lib/deploy/stepFunctions/compileIamRole.js

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function getTaskStates(states) {
1616
const parallelStates = _.flatMap(state.Branches, branch => _.values(branch.States));
1717
return getTaskStates(parallelStates);
1818
}
19+
case 'Map': {
20+
const mapStates = state.Iterator.States;
21+
return getTaskStates(mapStates);
22+
}
1923
default: {
2024
return [];
2125
}

lib/deploy/stepFunctions/compileIamRole.test.js

+120-12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ describe('#compileIamRole', () => {
3333
expect(statements[0].Resource).to.equal('*');
3434
};
3535

36+
const getAlias = functionArn => ({
37+
'Fn::Sub': [
38+
'${functionArn}:*',
39+
{
40+
functionArn,
41+
},
42+
],
43+
});
44+
3645
it('should do nothing when role property exists in all statmachine properties', () => {
3746
serverless.service.stepFunctions = {
3847
stateMachines: {
@@ -1429,19 +1438,9 @@ describe('#compileIamRole', () => {
14291438

14301439
const expectedResources = [
14311440
'arn:aws:lambda:region:accountId:function:with-alias',
1432-
{
1433-
'Fn::Sub': [
1434-
'${functionArn}:*',
1435-
{ functionArn: 'arn:aws:lambda:region:accountId:function:with-alias' },
1436-
],
1437-
},
1441+
getAlias('arn:aws:lambda:region:accountId:function:with-alias'),
14381442
'arn:aws:lambda:region:accountId:function:no-alias',
1439-
{
1440-
'Fn::Sub': [
1441-
'${functionArn}:*',
1442-
{ functionArn: 'arn:aws:lambda:region:accountId:function:no-alias' },
1443-
],
1444-
},
1443+
getAlias('arn:aws:lambda:region:accountId:function:no-alias'),
14451444
];
14461445
expect(lambdaPermissions[0].Resource).to.deep.include.members(expectedResources);
14471446
});
@@ -1568,4 +1567,113 @@ describe('#compileIamRole', () => {
15681567
expect(stateMachinePermissions).to.have.lengthOf(1);
15691568
expect(stateMachinePermissions[0].Resource).to.equal('*');
15701569
});
1570+
1571+
it('should support Map state type', () => {
1572+
const getStateMachine = (name, lambdaArn) => ({
1573+
name,
1574+
definition: {
1575+
StartAt: 'A',
1576+
States: {
1577+
A: {
1578+
Type: 'Map',
1579+
Iterator: {
1580+
StartAt: 'B',
1581+
States: {
1582+
B: {
1583+
Type: 'Task',
1584+
Resource: lambdaArn,
1585+
End: true,
1586+
},
1587+
},
1588+
},
1589+
End: true,
1590+
},
1591+
},
1592+
},
1593+
});
1594+
1595+
serverless.service.stepFunctions = {
1596+
stateMachines: {
1597+
myStateMachine: getStateMachine('sm1', 'arn:aws:lambda:us-west-2:1234567890:function:foo'),
1598+
},
1599+
};
1600+
1601+
serverlessStepFunctions.compileIamRole();
1602+
const statements = serverlessStepFunctions.serverless.service
1603+
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
1604+
.Properties.Policies[0].PolicyDocument.Statement;
1605+
1606+
const lambdaPermissions = statements.filter(s => _.isEqual(s.Action, ['lambda:InvokeFunction']));
1607+
expect(lambdaPermissions).to.have.lengthOf(1);
1608+
1609+
const lambdaArns = [
1610+
'arn:aws:lambda:us-west-2:1234567890:function:foo',
1611+
getAlias('arn:aws:lambda:us-west-2:1234567890:function:foo'),
1612+
];
1613+
expect(lambdaPermissions[0].Resource).to.deep.equal(lambdaArns);
1614+
});
1615+
1616+
it('should support nested Map state type', () => {
1617+
const getStateMachine = (name, lambdaArn1, lambdaArn2) => ({
1618+
name,
1619+
definition: {
1620+
StartAt: 'A',
1621+
States: {
1622+
A: {
1623+
Type: 'Map',
1624+
Iterator: {
1625+
StartAt: 'B',
1626+
States: {
1627+
B: {
1628+
Type: 'Task',
1629+
Resource: lambdaArn1,
1630+
Next: 'C',
1631+
},
1632+
C: {
1633+
Type: 'Map',
1634+
Iterator: {
1635+
StartAt: 'D',
1636+
States: {
1637+
D: {
1638+
Type: 'Task',
1639+
Resource: lambdaArn2,
1640+
End: true,
1641+
},
1642+
},
1643+
},
1644+
End: true,
1645+
},
1646+
},
1647+
},
1648+
End: true,
1649+
},
1650+
},
1651+
},
1652+
});
1653+
1654+
const lambdaArn1 = 'arn:aws:lambda:us-west-2:1234567890:function:foo';
1655+
const lambdaArn2 = 'arn:aws:lambda:us-west-2:1234567890:function:bar';
1656+
1657+
serverless.service.stepFunctions = {
1658+
stateMachines: {
1659+
myStateMachine: getStateMachine('sm1', lambdaArn1, lambdaArn2),
1660+
},
1661+
};
1662+
1663+
serverlessStepFunctions.compileIamRole();
1664+
const statements = serverlessStepFunctions.serverless.service
1665+
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
1666+
.Properties.Policies[0].PolicyDocument.Statement;
1667+
1668+
const lambdaPermissions = statements.filter(s => _.isEqual(s.Action, ['lambda:InvokeFunction']));
1669+
expect(lambdaPermissions).to.have.lengthOf(1);
1670+
1671+
const lambdaArns = [
1672+
lambdaArn1,
1673+
getAlias(lambdaArn1),
1674+
lambdaArn2,
1675+
getAlias(lambdaArn2),
1676+
];
1677+
expect(lambdaPermissions[0].Resource).to.deep.equal(lambdaArns);
1678+
});
15711679
});

0 commit comments

Comments
 (0)