Skip to content

Commit 8927bf8

Browse files
fic(core): Fixing TGW Cross account attachment (#732)
* Fixing TGW Corss account attachment * fixing tests * fixing import * fix prettier version with 2.2.0 which is using in code
1 parent 7656059 commit 8927bf8

File tree

8 files changed

+59
-5
lines changed

8 files changed

+59
-5
lines changed

.github/workflows/lint-prettier.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
node-version: 12
4646
- name: Install Prettier
4747
run: |
48-
npm install -g prettier
48+
npm install -g prettier@2.2.0
4949
- name: Analyze TypeScript files
5050
run: |
5151
prettier --check **/*.ts

src/deployments/cdk/src/apps/phase-1.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as transitGateway from '../deployments/transit-gateway';
3434
import * as centralEndpoints from '../deployments/central-endpoints';
3535
import { CfnResourceStackCleanupOutput } from '../deployments/cleanup/outputs';
3636
import { VpcOutputFinder, VpcSubnetOutput } from '@aws-accelerator/common-outputs/src/vpc';
37+
import { TransitGatewayAttachmentOutputFinder } from '@aws-accelerator/common-outputs/src/transit-gateway';
3738

3839
export interface IamPolicyArtifactsOutput {
3940
bucketArn: string;
@@ -194,6 +195,9 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
194195

195196
const subscriptionCheckDone: string[] = [];
196197
const dnsLogGroupsAccountAndRegion: { [accoutKey: string]: boolean } = {};
198+
const existingAttachments = TransitGatewayAttachmentOutputFinder.findAll({
199+
outputs,
200+
});
197201
// Create all the VPCs for accounts and organizational units
198202
for (const { ouKey, accountKey, vpcConfig, deployments } of acceleratorConfig.getVpcConfigs()) {
199203
let createPolicy = false;
@@ -230,6 +234,7 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
230234
acceleratorName,
231235
installerVersion,
232236
vpcOutput,
237+
existingAttachments,
233238
});
234239

235240
const pcxConfig = vpcConfig.pcx;

src/deployments/cdk/src/common/vpc.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { NestedStack } from '@aws-cdk/aws-cloudformation';
1313
import { SecurityGroup } from './security-group';
1414
import { StackOutput } from '@aws-accelerator/common-outputs/src/stack-output';
1515
import { 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';
1721
import { CfnTransitGatewayAttachmentOutput } from '../deployments/transit-gateway/outputs';
1822
import { AddTagsToResourcesOutput } from './add-tags-to-resources-output';
1923
import { 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
}

src/deployments/cdk/src/deployments/transit-gateway/step-2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function step2(props: TransitGatewayStep2Props) {
2626
continue;
2727
}
2828

29-
new TransitGatewayRoute(accountStack, `TgwRoute${index}`, {
29+
new TransitGatewayRoute(accountStack, `TgwRoute${attachment.constructIndex || index}`, {
3030
tgwAttachmentId: attachment.tgwAttachmentId,
3131
tgwRouteAssociates: attachment.tgwRouteAssociates,
3232
tgwRoutePropagates: attachment.tgwRoutePropagates,

src/deployments/cdk/test/common/vpc.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ test('the VPC creation should create the correct amount of subnets', () => {
143143
outputs: [],
144144
acceleratorName: 'test',
145145
installerVersion: '0.0.0',
146+
existingAttachments: [],
146147
});
147148

148149
// Convert the stack to a CloudFormation template
@@ -264,6 +265,7 @@ test('the VPC creation should throw an error when a subnet uses a route table th
264265
outputs: [],
265266
acceleratorName: 'test',
266267
installerVersion: '0.0.0',
268+
existingAttachments: [],
267269
});
268270
});
269271
});
@@ -292,6 +294,7 @@ test('the VPC creation should create the internet gateway', () => {
292294
outputs: [],
293295
acceleratorName: 'test',
294296
installerVersion: '0.0.0',
297+
existingAttachments: [],
295298
});
296299

297300
// Convert the stack to a CloudFormation template
@@ -328,6 +331,7 @@ test('the VPC creation should create the VPN gateway', () => {
328331
outputs: [],
329332
acceleratorName: 'test',
330333
installerVersion: '0.0.0',
334+
existingAttachments: [],
331335
});
332336

333337
// Convert the stack to a CloudFormation template
@@ -453,6 +457,7 @@ test('the VPC creation should create the NAT gateway', () => {
453457
outputs: [],
454458
acceleratorName: 'test',
455459
installerVersion: '0.0.0',
460+
existingAttachments: [],
456461
});
457462

458463
// Convert the stack to a CloudFormation template

src/installer/cdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async function main() {
193193
},
194194
}),
195195
environment: {
196-
buildImage: codebuild.LinuxBuildImage.STANDARD_3_0,
196+
buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
197197
privileged: true, // Allow access to the Docker daemon
198198
computeType: codebuild.ComputeType.MEDIUM,
199199
environmentVariables: {

src/lib/cdk-accelerator/src/codebuild/cdk-deploy-project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class CdkDeployProject extends CdkDeployProjectBase {
102102
path: projectAsset.s3ObjectKey,
103103
}),
104104
environment: {
105-
buildImage: codebuild.LinuxBuildImage.STANDARD_4_0,
105+
buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
106106
computeType: computeType ?? codebuild.ComputeType.MEDIUM,
107107
environmentVariables: this.environmentVariables,
108108
},

src/lib/common-outputs/src/transit-gateway.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { optional } from '@aws-accelerator/common-types';
12
import * as t from 'io-ts';
23
import { StackOutput } from './stack-output';
34
import { createStructuredOutputFinder } from './structured-output';
@@ -34,6 +35,8 @@ export const TransitGatewayAttachmentOutput = t.interface(
3435
tgwRoutePropagates: t.array(t.string),
3536
blackhole: t.boolean,
3637
cidr: t.string,
38+
vpc: optional(t.string),
39+
constructIndex: optional(t.string),
3740
},
3841
'TgwAttachmentOutput',
3942
);

0 commit comments

Comments
 (0)