@@ -8,6 +8,7 @@ import * as kms from '@aws-cdk/aws-kms';
8
8
import * as logs from '@aws-cdk/aws-logs' ;
9
9
import * as s3 from '@aws-cdk/aws-s3' ;
10
10
import * as signer from '@aws-cdk/aws-signer' ;
11
+ import * as sns from '@aws-cdk/aws-sns' ;
11
12
import * as sqs from '@aws-cdk/aws-sqs' ;
12
13
import { testDeprecated } from '@aws-cdk/cdk-build-tools' ;
13
14
import * as cdk from '@aws-cdk/core' ;
@@ -684,6 +685,84 @@ describe('function', () => {
684
685
} ) ) . toThrow ( / d e a d L e t t e r Q u e u e d e f i n e d b u t d e a d L e t t e r Q u e u e E n a b l e d e x p l i c i t l y s e t t o f a l s e / ) ;
685
686
} ) ;
686
687
688
+ test ( 'default function with SNS DLQ when client provides Topic to be used as DLQ' , ( ) => {
689
+ const stack = new cdk . Stack ( ) ;
690
+
691
+ const dlTopic = new sns . Topic ( stack , 'DeadLetterTopic' ) ;
692
+
693
+ new lambda . Function ( stack , 'MyLambda' , {
694
+ code : new lambda . InlineCode ( 'foo' ) ,
695
+ handler : 'index.handler' ,
696
+ runtime : lambda . Runtime . NODEJS_10_X ,
697
+ deadLetterTopic : dlTopic ,
698
+ } ) ;
699
+
700
+ const template = Template . fromStack ( stack ) ;
701
+ template . hasResourceProperties ( 'AWS::IAM::Policy' , {
702
+ PolicyDocument : {
703
+ Statement : Match . arrayWith ( [
704
+ {
705
+ Action : 'sns:Publish' ,
706
+ Effect : 'Allow' ,
707
+ Resource : {
708
+ Ref : 'DeadLetterTopicC237650B' ,
709
+ } ,
710
+ } ,
711
+ ] ) ,
712
+ } ,
713
+ } ) ;
714
+ template . hasResourceProperties ( 'AWS::Lambda::Function' , {
715
+ DeadLetterConfig : {
716
+ TargetArn : {
717
+ Ref : 'DeadLetterTopicC237650B' ,
718
+ } ,
719
+ } ,
720
+ } ) ;
721
+ } ) ;
722
+
723
+ test ( 'error when default function with SNS DLQ when client provides Topic to be used as DLQ and deadLetterQueueEnabled set to false' , ( ) => {
724
+ const stack = new cdk . Stack ( ) ;
725
+
726
+ const dlTopic = new sns . Topic ( stack , 'DeadLetterTopic' ) ;
727
+
728
+ expect ( ( ) => new lambda . Function ( stack , 'MyLambda' , {
729
+ code : new lambda . InlineCode ( 'foo' ) ,
730
+ handler : 'index.handler' ,
731
+ runtime : lambda . Runtime . NODEJS_10_X ,
732
+ deadLetterQueueEnabled : false ,
733
+ deadLetterTopic : dlTopic ,
734
+ } ) ) . toThrow ( / d e a d L e t t e r Q u e u e a n d d e a d L e t t e r T o p i c c a n n o t b e s p e c i f i e d t o g e t h e r a t t h e s a m e t i m e / ) ;
735
+ } ) ;
736
+
737
+ test ( 'error when default function with SNS DLQ when client provides Topic to be used as DLQ and deadLetterQueueEnabled set to true' , ( ) => {
738
+ const stack = new cdk . Stack ( ) ;
739
+
740
+ const dlTopic = new sns . Topic ( stack , 'DeadLetterTopic' ) ;
741
+
742
+ expect ( ( ) => new lambda . Function ( stack , 'MyLambda' , {
743
+ code : new lambda . InlineCode ( 'foo' ) ,
744
+ handler : 'index.handler' ,
745
+ runtime : lambda . Runtime . NODEJS_10_X ,
746
+ deadLetterQueueEnabled : true ,
747
+ deadLetterTopic : dlTopic ,
748
+ } ) ) . toThrow ( / d e a d L e t t e r Q u e u e a n d d e a d L e t t e r T o p i c c a n n o t b e s p e c i f i e d t o g e t h e r a t t h e s a m e t i m e / ) ;
749
+ } ) ;
750
+
751
+ test ( 'error when both topic and queue are presented as DLQ' , ( ) => {
752
+ const stack = new cdk . Stack ( ) ;
753
+
754
+ const dlQueue = new sqs . Queue ( stack , 'DLQ' ) ;
755
+ const dlTopic = new sns . Topic ( stack , 'DeadLetterTopic' ) ;
756
+
757
+ expect ( ( ) => new lambda . Function ( stack , 'MyLambda' , {
758
+ code : new lambda . InlineCode ( 'foo' ) ,
759
+ handler : 'index.handler' ,
760
+ runtime : lambda . Runtime . NODEJS_10_X ,
761
+ deadLetterQueue : dlQueue ,
762
+ deadLetterTopic : dlTopic ,
763
+ } ) ) . toThrow ( / d e a d L e t t e r Q u e u e a n d d e a d L e t t e r T o p i c c a n n o t b e s p e c i f i e d t o g e t h e r a t t h e s a m e t i m e / ) ;
764
+ } ) ;
765
+
687
766
test ( 'default function with Active tracing' , ( ) => {
688
767
const stack = new cdk . Stack ( ) ;
689
768
@@ -1561,7 +1640,7 @@ describe('function', () => {
1561
1640
expect ( logGroup . logGroupArn ) . toBeDefined ( ) ;
1562
1641
} ) ;
1563
1642
1564
- test ( 'dlq is returned when provided by user' , ( ) => {
1643
+ test ( 'dlq is returned when provided by user and is Queue ' , ( ) => {
1565
1644
const stack = new cdk . Stack ( ) ;
1566
1645
1567
1646
const dlQueue = new sqs . Queue ( stack , 'DeadLetterQueue' , {
@@ -1576,12 +1655,37 @@ describe('function', () => {
1576
1655
deadLetterQueue : dlQueue ,
1577
1656
} ) ;
1578
1657
const deadLetterQueue = fn . deadLetterQueue ;
1579
- expect ( deadLetterQueue ?. queueArn ) . toBeDefined ( ) ;
1580
- expect ( deadLetterQueue ?. queueName ) . toBeDefined ( ) ;
1581
- expect ( deadLetterQueue ?. queueUrl ) . toBeDefined ( ) ;
1658
+ const deadLetterTopic = fn . deadLetterTopic ;
1659
+
1660
+ expect ( deadLetterTopic ) . toBeUndefined ( ) ;
1661
+
1662
+ expect ( deadLetterQueue ) . toBeDefined ( ) ;
1663
+ expect ( deadLetterQueue ) . toBeInstanceOf ( sqs . Queue ) ;
1582
1664
} ) ;
1583
1665
1584
- test ( 'dlq is returned when setup by cdk' , ( ) => {
1666
+ test ( 'dlq is returned when provided by user and is Topic' , ( ) => {
1667
+ const stack = new cdk . Stack ( ) ;
1668
+
1669
+ const dlTopic = new sns . Topic ( stack , 'DeadLetterQueue' , {
1670
+ topicName : 'MyLambda_DLQ' ,
1671
+ } ) ;
1672
+
1673
+ const fn = new lambda . Function ( stack , 'fn' , {
1674
+ handler : 'foo' ,
1675
+ runtime : lambda . Runtime . NODEJS_10_X ,
1676
+ code : lambda . Code . fromInline ( 'foo' ) ,
1677
+ deadLetterTopic : dlTopic ,
1678
+ } ) ;
1679
+ const deadLetterQueue = fn . deadLetterQueue ;
1680
+ const deadLetterTopic = fn . deadLetterTopic ;
1681
+
1682
+ expect ( deadLetterQueue ) . toBeUndefined ( ) ;
1683
+
1684
+ expect ( deadLetterTopic ) . toBeDefined ( ) ;
1685
+ expect ( deadLetterTopic ) . toBeInstanceOf ( sns . Topic ) ;
1686
+ } ) ;
1687
+
1688
+ test ( 'dlq is returned when setup by cdk and is Queue' , ( ) => {
1585
1689
const stack = new cdk . Stack ( ) ;
1586
1690
const fn = new lambda . Function ( stack , 'fn' , {
1587
1691
handler : 'foo' ,
@@ -1590,9 +1694,12 @@ describe('function', () => {
1590
1694
deadLetterQueueEnabled : true ,
1591
1695
} ) ;
1592
1696
const deadLetterQueue = fn . deadLetterQueue ;
1593
- expect ( deadLetterQueue ?. queueArn ) . toBeDefined ( ) ;
1594
- expect ( deadLetterQueue ?. queueName ) . toBeDefined ( ) ;
1595
- expect ( deadLetterQueue ?. queueUrl ) . toBeDefined ( ) ;
1697
+ const deadLetterTopic = fn . deadLetterTopic ;
1698
+
1699
+ expect ( deadLetterTopic ) . toBeUndefined ( ) ;
1700
+
1701
+ expect ( deadLetterQueue ) . toBeDefined ( ) ;
1702
+ expect ( deadLetterQueue ) . toBeInstanceOf ( sqs . Queue ) ;
1596
1703
} ) ;
1597
1704
1598
1705
test ( 'dlq is undefined when not setup' , ( ) => {
@@ -1603,7 +1710,10 @@ describe('function', () => {
1603
1710
code : lambda . Code . fromInline ( 'foo' ) ,
1604
1711
} ) ;
1605
1712
const deadLetterQueue = fn . deadLetterQueue ;
1713
+ const deadLetterTopic = fn . deadLetterTopic ;
1714
+
1606
1715
expect ( deadLetterQueue ) . toBeUndefined ( ) ;
1716
+ expect ( deadLetterTopic ) . toBeUndefined ( ) ;
1607
1717
} ) ;
1608
1718
1609
1719
test ( 'one and only one child LogRetention construct will be created' , ( ) => {
0 commit comments