@@ -513,4 +513,124 @@ describe('EC2 Routing', () => {
513
513
} ) ;
514
514
} ) ;
515
515
516
+ describe ( 'VPCPeeringConnection' , ( ) => {
517
+ test ( 'Cross account peering connection' , ( ) => {
518
+ const vpc1 = new vpc . VpcV2 ( stack , 'VPC1' , {
519
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.1.0.0/16' ) ,
520
+ } ) ;
521
+ const vpc2 = new vpc . VpcV2 ( stack , 'VPC2' , {
522
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.2.0.0/16' ) ,
523
+ } ) ;
524
+
525
+ new route . VPCPeeringConnection ( stack , 'TestPeering' , {
526
+ isCrossAccount : true ,
527
+ requestorVpc : vpc1 ,
528
+ acceptorVpc : vpc2 ,
529
+ acceptorAccountId : '123456789012' ,
530
+ acceptorRegion : 'us-west-2' ,
531
+ } ) ;
532
+
533
+ const template = Template . fromStack ( stack ) ;
534
+ template . hasResourceProperties ( 'AWS::EC2::VPCPeeringConnection' , {
535
+ PeerOwnerId : '123456789012' ,
536
+ PeerRegion : 'us-west-2' ,
537
+ VpcId : {
538
+ 'Fn::GetAtt' : [ 'VPC17DE2CF87' , 'VpcId' ] ,
539
+ } ,
540
+ PeerVpcId : {
541
+ 'Fn::GetAtt' : [ 'VPC2C1F0E711' , 'VpcId' ] ,
542
+ } ,
543
+ PeerRoleArn : {
544
+ 'Fn::GetAtt' : [ 'TestPeeringPeerRole67D845E9' , 'Arn' ] ,
545
+ } ,
546
+ } ) ;
547
+ } ) ;
548
+
549
+ test ( 'CIDR block overlap should throw error' , ( ) => {
550
+ const vpc1 = new vpc . VpcV2 ( stack , 'VPC1' , {
551
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.0.0.0/16' ) ,
552
+ } ) ;
553
+
554
+ const vpc2 = new vpc . VpcV2 ( stack , 'VPC2' , {
555
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.0.0.0/16' ) ,
556
+ } ) ;
557
+
558
+ expect ( ( ) => {
559
+ new route . VPCPeeringConnection ( stack , 'TestPeering' , {
560
+ isCrossAccount : false ,
561
+ requestorVpc : vpc1 ,
562
+ acceptorVpc : vpc2 ,
563
+ } ) ;
564
+ } ) . toThrow ( / C I D R b l o c k s h o u l d n o t o v e r l a p w i t h e x i s t i n g s u b n e t b l o c k s / ) ;
565
+ } ) ;
566
+
567
+ test ( 'CIDR block overlap with secondary CIDR block should throw error' , ( ) => {
568
+ const vpc1 = new vpc . VpcV2 ( stack , 'VPC1' , {
569
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.0.0.0/16' ) ,
570
+ secondaryAddressBlocks : [ vpc . IpAddresses . ipv4 ( '10.1.0.0/16' , { cidrBlockName : 'Temp Block' } ) ] ,
571
+ } ) ;
572
+
573
+ const vpc2 = new vpc . VpcV2 ( stack , 'VPC2' , {
574
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.2.0.0/16' ) ,
575
+ secondaryAddressBlocks : [ vpc . IpAddresses . ipv4 ( '10.1.0.0/16' , { cidrBlockName : 'Temp Block' } ) ] ,
576
+ } ) ;
577
+
578
+ expect ( ( ) => {
579
+ new route . VPCPeeringConnection ( stack , 'TestPeering' , {
580
+ isCrossAccount : false ,
581
+ requestorVpc : vpc1 ,
582
+ acceptorVpc : vpc2 ,
583
+ } ) ;
584
+ } ) . toThrow ( / C I D R b l o c k s h o u l d n o t o v e r l a p w i t h e x i s t i n g s u b n e t b l o c k s / ) ;
585
+ } ) ;
586
+
587
+ test ( 'Non overlapping CIDR blocks should succeed' , ( ) => {
588
+ const vpc1 = new vpc . VpcV2 ( stack , 'VPC1' , {
589
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.0.0.0/16' ) ,
590
+ secondaryAddressBlocks : [ vpc . IpAddresses . ipv4 ( '10.1.0.0/16' , { cidrBlockName : 'Temp Block' } ) ] ,
591
+ } ) ;
592
+
593
+ const vpc2 = new vpc . VpcV2 ( stack , 'VPC2' , {
594
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.2.0.0/16' ) ,
595
+ secondaryAddressBlocks : [ vpc . IpAddresses . ipv4 ( '10.3.0.0/16' , { cidrBlockName : 'Temp Block' } ) ] ,
596
+ } ) ;
597
+
598
+ expect ( ( ) => {
599
+ new route . VPCPeeringConnection ( stack , 'TestPeering' , {
600
+ isCrossAccount : false ,
601
+ requestorVpc : vpc1 ,
602
+ acceptorVpc : vpc2 ,
603
+ } ) ;
604
+ } ) . not . toThrow ( ) ;
605
+ } ) ;
606
+
607
+ test ( 'Default region handling for same account peering' , ( ) => {
608
+ const vpc1 = new vpc . VpcV2 ( stack , 'VPC1' , {
609
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.1.0.0/16' ) ,
610
+ } ) ;
611
+
612
+ const vpc2 = new vpc . VpcV2 ( stack , 'VPC2' , {
613
+ primaryAddressBlock : vpc . IpAddresses . ipv4 ( '10.2.0.0/16' ) ,
614
+ } ) ;
615
+
616
+ new route . VPCPeeringConnection ( stack , 'TestPeering' , {
617
+ isCrossAccount : false ,
618
+ requestorVpc : vpc1 ,
619
+ acceptorVpc : vpc2 ,
620
+ } ) ;
621
+
622
+ const template = Template . fromStack ( stack ) ;
623
+ template . hasResourceProperties ( 'AWS::EC2::VPCPeeringConnection' , {
624
+ VpcId : {
625
+ 'Fn::GetAtt' : [ 'VPC17DE2CF87' , 'VpcId' ] ,
626
+ } ,
627
+ PeerVpcId : {
628
+ 'Fn::GetAtt' : [ 'VPC2C1F0E711' , 'VpcId' ] ,
629
+ } ,
630
+ PeerRegion : {
631
+ Ref : 'AWS::Region' , // CDK resolves this as the stack region
632
+ } ,
633
+ } ) ;
634
+ } ) ;
635
+ } ) ;
516
636
} ) ;
0 commit comments