@@ -576,6 +576,91 @@ describe('#compileIamRole', () => {
576
576
. to . be . deep . equal ( [ worldTableArn ] ) ;
577
577
} ) ;
578
578
579
+ it ( 'should give dynamodb permission for table name imported from external stack' , ( ) => {
580
+ const externalHelloTable = { 'Fn::ImportValue' : 'HelloStack:Table:Name' } ;
581
+ const helloTableArn = {
582
+ 'Fn::Join' : [
583
+ ':' , [ 'arn:aws:dynamodb' , { Ref : 'AWS::Region' } , { Ref : 'AWS::AccountId' } , { 'Fn::Join' : [ '/' , [ 'table' , externalHelloTable ] ] } ] ,
584
+ ] ,
585
+ } ;
586
+
587
+ const externalWorldTable = { 'Fn::ImportValue' : 'WorldStack:Table:Name' } ;
588
+ const worldTableArn = {
589
+ 'Fn::Join' : [
590
+ ':' , [ 'arn:aws:dynamodb' , { Ref : 'AWS::Region' } , { Ref : 'AWS::AccountId' } , { 'Fn::Join' : [ '/' , [ 'table' , externalWorldTable ] ] } ] ,
591
+ ] ,
592
+ } ;
593
+
594
+ const genStateMachine = ( id , tableName ) => ( {
595
+ id,
596
+ definition : {
597
+ StartAt : 'A' ,
598
+ States : {
599
+ A : {
600
+ Type : 'Task' ,
601
+ Resource : 'arn:aws:states:::dynamodb:updateItem' ,
602
+ Parameters : {
603
+ TableName : tableName ,
604
+ } ,
605
+ Next : 'B' ,
606
+ } ,
607
+ B : {
608
+ Type : 'Task' ,
609
+ Resource : 'arn:aws:states:::dynamodb:putItem' ,
610
+ Parameters : {
611
+ TableName : tableName ,
612
+ } ,
613
+ Next : 'C' ,
614
+ } ,
615
+ C : {
616
+ Type : 'Task' ,
617
+ Resource : 'arn:aws:states:::dynamodb:getItem' ,
618
+ Parameters : {
619
+ TableName : tableName ,
620
+ } ,
621
+ Next : 'D' ,
622
+ } ,
623
+ D : {
624
+ Type : 'Task' ,
625
+ Resource : 'arn:aws:states:::dynamodb:deleteItem' ,
626
+ Parameters : {
627
+ TableName : tableName ,
628
+ } ,
629
+ End : true ,
630
+ } ,
631
+ } ,
632
+ } ,
633
+ } ) ;
634
+
635
+ serverless . service . stepFunctions = {
636
+ stateMachines : {
637
+ myStateMachine1 : genStateMachine ( 'StateMachine1' , externalHelloTable ) ,
638
+ myStateMachine2 : genStateMachine ( 'StateMachine2' , externalWorldTable ) ,
639
+ } ,
640
+ } ;
641
+
642
+ serverlessStepFunctions . compileIamRole ( ) ;
643
+ const resources = serverlessStepFunctions . serverless . service
644
+ . provider . compiledCloudFormationTemplate . Resources ;
645
+ const policy1 = resources . StateMachine1Role . Properties . Policies [ 0 ] ;
646
+ const policy2 = resources . StateMachine2Role . Properties . Policies [ 0 ] ;
647
+
648
+ [ policy1 , policy2 ] . forEach ( ( policy ) => {
649
+ expect ( policy . PolicyDocument . Statement [ 0 ] . Action )
650
+ . to . be . deep . equal ( [
651
+ 'dynamodb:UpdateItem' ,
652
+ 'dynamodb:PutItem' ,
653
+ 'dynamodb:GetItem' ,
654
+ 'dynamodb:DeleteItem' ,
655
+ ] ) ;
656
+ } ) ;
657
+
658
+ expect ( policy1 . PolicyDocument . Statement [ 0 ] . Resource )
659
+ . to . be . deep . equal ( [ helloTableArn ] ) ;
660
+ expect ( policy2 . PolicyDocument . Statement [ 0 ] . Resource )
661
+ . to . be . deep . equal ( [ worldTableArn ] ) ;
662
+ } ) ;
663
+
579
664
it ( 'should give dynamodb permission to * whenever TableName.$ is seen' , ( ) => {
580
665
const helloTable = 'hello' ;
581
666
0 commit comments