@@ -33,6 +33,15 @@ describe('#compileIamRole', () => {
33
33
expect ( statements [ 0 ] . Resource ) . to . equal ( '*' ) ;
34
34
} ;
35
35
36
+ const getAlias = functionArn => ( {
37
+ 'Fn::Sub' : [
38
+ '${functionArn}:*' ,
39
+ {
40
+ functionArn,
41
+ } ,
42
+ ] ,
43
+ } ) ;
44
+
36
45
it ( 'should do nothing when role property exists in all statmachine properties' , ( ) => {
37
46
serverless . service . stepFunctions = {
38
47
stateMachines : {
@@ -1429,19 +1438,9 @@ describe('#compileIamRole', () => {
1429
1438
1430
1439
const expectedResources = [
1431
1440
'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' ) ,
1438
1442
'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' ) ,
1445
1444
] ;
1446
1445
expect ( lambdaPermissions [ 0 ] . Resource ) . to . deep . include . members ( expectedResources ) ;
1447
1446
} ) ;
@@ -1568,4 +1567,113 @@ describe('#compileIamRole', () => {
1568
1567
expect ( stateMachinePermissions ) . to . have . lengthOf ( 1 ) ;
1569
1568
expect ( stateMachinePermissions [ 0 ] . Resource ) . to . equal ( '*' ) ;
1570
1569
} ) ;
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
+ } ) ;
1571
1679
} ) ;
0 commit comments