@@ -13,7 +13,11 @@ import { NestedStack } from '@aws-cdk/aws-cloudformation';
1313import { SecurityGroup } from './security-group' ;
1414import { StackOutput } from '@aws-accelerator/common-outputs/src/stack-output' ;
1515import { AccountStacks } from '../common/account-stacks' ;
16- import { TransitGatewayOutputFinder , TransitGatewayOutput } from '@aws-accelerator/common-outputs/src/transit-gateway' ;
16+ import {
17+ TransitGatewayOutputFinder ,
18+ TransitGatewayOutput ,
19+ TransitGatewayAttachmentOutput ,
20+ } from '@aws-accelerator/common-outputs/src/transit-gateway' ;
1721import { CfnTransitGatewayAttachmentOutput } from '../deployments/transit-gateway/outputs' ;
1822import { AddTagsToResourcesOutput } from './add-tags-to-resources-output' ;
1923import { VpcDefaultSecurityGroup } from '@aws-accelerator/custom-resource-vpc-default-security-group' ;
@@ -102,6 +106,7 @@ export interface VpcProps extends VpcCommonProps {
102106 outputs : StackOutput [ ] ;
103107 acceleratorName : string ;
104108 installerVersion : string ;
109+ existingAttachments : TransitGatewayAttachmentOutput [ ] ;
105110 vpcOutput ?: VpcOutput ;
106111}
107112
@@ -156,6 +161,7 @@ export class Vpc extends cdk.Construct implements constructs.Vpc {
156161 acceleratorName,
157162 installerVersion,
158163 vpcOutput,
164+ existingAttachments,
159165 } = props . vpcProps ;
160166 const vpcName = props . vpcProps . vpcConfig . name ;
161167
@@ -421,6 +427,39 @@ export class Vpc extends cdk.Construct implements constructs.Vpc {
421427 cidr : this . cidrBlock ,
422428 } ) ;
423429 } else {
430+ let constructIndex : string ;
431+ let existingAttachment : TransitGatewayAttachmentOutput | undefined ;
432+ existingAttachment = existingAttachments . find (
433+ att =>
434+ att . accountKey === tgwAttach . account &&
435+ att . region === this . region &&
436+ att . cidr === this . cidrBlock &&
437+ att . vpc === vpcName ,
438+ ) ;
439+ if ( ! existingAttachment ) {
440+ existingAttachment = existingAttachments . find (
441+ att => att . accountKey === tgwAttach . account && att . region === this . region && att . cidr === this . cidrBlock ,
442+ ) ;
443+ }
444+ if ( ! existingAttachment ) {
445+ // Generate hash
446+ constructIndex = hashSum ( {
447+ accountKey : tgwAttach . account ,
448+ rgion : this . region ,
449+ cidr : this . cidrBlock ,
450+ vpc : vpcName ,
451+ } ) ;
452+ } else {
453+ // This might cause failure if existing users already having multiple tgw cross account attachments in same account and region
454+ constructIndex =
455+ existingAttachment . constructIndex ||
456+ existingAttachments
457+ . findIndex (
458+ att =>
459+ att . accountKey === tgwAttach . account && att . region === this . region && att . cidr === this . cidrBlock ,
460+ )
461+ . toString ( ) ;
462+ }
424463 new CfnTransitGatewayAttachmentOutput ( this , 'TgwAttachmentOutput' , {
425464 accountKey : tgwAttach . account ,
426465 region : this . region ,
@@ -429,6 +468,8 @@ export class Vpc extends cdk.Construct implements constructs.Vpc {
429468 tgwRoutePropagates,
430469 blackhole : blackhole ?? false ,
431470 cidr : this . cidrBlock ,
471+ vpc : vpcName ,
472+ constructIndex,
432473 } ) ;
433474 }
434475 }
0 commit comments