Skip to content

Commit c3ce485

Browse files
fix(core): Fix creating TGW Static Routes (#735)
* Fixing TGW Static Route * Fixing tgw routes
1 parent 8927bf8 commit c3ce485

File tree

2 files changed

+95
-56
lines changed
  • src
    • deployments/cdk/src/deployments/transit-gateway
    • lib/custom-resources/cdk-guardduty-admin-setup/runtime/src

2 files changed

+95
-56
lines changed

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

Lines changed: 93 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ export async function step3(props: TransitGatewayStep3Props) {
2626
const tgwPeeringAttachmentOutputs = TransitGatewayPeeringAttachmentOutputFinder.findAll({
2727
outputs,
2828
});
29-
if (tgwPeeringAttachmentOutputs.length === 0) {
30-
return;
31-
}
3229

3330
const accountConfigs = config.getAccountConfigs();
3431
for (const [accountKey, accountConfig] of accountConfigs) {
@@ -44,6 +41,7 @@ export async function step3(props: TransitGatewayStep3Props) {
4441
name: tgwConfig.name,
4542
});
4643
if (!transitGateway) {
44+
console.warn(`TGW not found "${accountKey}/${tgwConfig.name}"`);
4745
continue;
4846
}
4947

@@ -57,9 +55,6 @@ export async function step3(props: TransitGatewayStep3Props) {
5755
const tgwPeer = output.tgws.find(tgw => tgw.name === tgwConfig.name && tgw.region === tgwConfig.region);
5856
return !!tgwPeer;
5957
});
60-
if (!tgwPeeringAttachment) {
61-
continue;
62-
}
6358

6459
if (!tgwConfig['tgw-routes']) {
6560
continue;
@@ -71,15 +66,19 @@ export async function step3(props: TransitGatewayStep3Props) {
7166
}
7267

7368
for (const route of tgwRoute.routes) {
74-
if (route['target-tgw'] || route['blackhole-route']) {
75-
CreateRoute(
76-
accountStack,
77-
route.destination,
78-
tgwRoute.name,
79-
tgwPeeringAttachment.tgwAttachmentId,
69+
if (route['target-tgw']) {
70+
if (!tgwPeeringAttachment) {
71+
console.warn(`No Peering Attachment found for "${tgwConfig.name}"`);
72+
continue;
73+
}
74+
CreateRoute({
75+
scope: accountStack,
76+
cidr: route.destination,
77+
routeName: tgwRoute.name,
8078
transitGateway,
81-
route['blackhole-route'],
82-
);
79+
attachmentId: tgwPeeringAttachment.tgwAttachmentId,
80+
blackhole: route['blackhole-route'],
81+
});
8382
} else if (route['target-vpc']) {
8483
const vpcOutput = VpcOutputFinder.tryFindOneByAccountAndRegionAndName({
8584
outputs,
@@ -92,14 +91,14 @@ export async function step3(props: TransitGatewayStep3Props) {
9291
continue;
9392
}
9493
const tgwAttachmentIds = vpcOutput.tgwAttachments.map(t => t.id);
95-
CreateRoutes(
96-
accountStack,
97-
route.destination,
98-
tgwRoute.name,
99-
tgwAttachmentIds,
94+
CreateRoutes({
95+
scope: accountStack,
96+
cidr: route.destination,
97+
routeName: tgwRoute.name,
10098
transitGateway,
101-
route['blackhole-route'],
102-
);
99+
attachmentIds: tgwAttachmentIds,
100+
blackhole: route['blackhole-route'],
101+
});
103102
} else if (route['target-vpn']) {
104103
const vpnAttachments = TgwVpnAttachmentsOutputFinder.tryFindOneByName({
105104
outputs,
@@ -117,14 +116,22 @@ export async function step3(props: TransitGatewayStep3Props) {
117116
if (!tgwAttachmentId) {
118117
continue;
119118
}
120-
CreateRoutes(
121-
accountStack,
122-
route.destination,
123-
tgwRoute.name,
124-
[tgwAttachmentId],
119+
CreateRoutes({
120+
scope: accountStack,
121+
cidr: route.destination,
122+
routeName: tgwRoute.name,
125123
transitGateway,
126-
route['blackhole-route'],
127-
);
124+
attachmentIds: [tgwAttachmentId],
125+
blackhole: route['blackhole-route'],
126+
});
127+
} else if (route['blackhole-route']) {
128+
CreateRoute({
129+
scope: accountStack,
130+
cidr: route.destination,
131+
routeName: tgwRoute.name,
132+
transitGateway,
133+
blackhole: route['blackhole-route'],
134+
});
128135
}
129136
}
130137
}
@@ -134,6 +141,13 @@ export async function step3(props: TransitGatewayStep3Props) {
134141
continue;
135142
}
136143

144+
if (!tgwPeeringAttachment) {
145+
console.warn(
146+
`No Peering Attachment found for "${accountKey}/${tgwConfig.name}". Skipping Create associations fot tgw-attach`,
147+
);
148+
continue;
149+
}
150+
137151
CreateAssociations(
138152
accountStacks,
139153
tgwPeeringAttachment,
@@ -164,35 +178,58 @@ export async function step3(props: TransitGatewayStep3Props) {
164178
}
165179
}
166180

167-
function CreateRoutes(
168-
scope: cdk.Construct,
169-
cidr: string,
170-
routeName: string,
171-
attachmentIds: string[],
172-
transitGateway: TransitGatewayOutput,
173-
blackhole?: boolean,
174-
) {
181+
function CreateRoutes(props: {
182+
scope: cdk.Construct;
183+
cidr: string;
184+
routeName: string;
185+
transitGateway: TransitGatewayOutput;
186+
attachmentIds?: string[];
187+
blackhole?: boolean;
188+
}) {
189+
const { cidr, routeName, scope, transitGateway, attachmentIds, blackhole } = props;
175190
for (const attachmentId of attachmentIds || []) {
176-
CreateRoute(scope, cidr, routeName, attachmentId, transitGateway, blackhole);
191+
CreateRoute({
192+
scope,
193+
cidr,
194+
routeName,
195+
transitGateway,
196+
attachmentId,
197+
blackhole,
198+
});
177199
}
178200
}
179201

180-
function CreateRoute(
181-
scope: cdk.Construct,
182-
cidr: string,
183-
routeName: string,
184-
attachmentId: string,
185-
transitGateway: TransitGatewayOutput,
186-
blackhole?: boolean,
187-
) {
202+
function CreateRoute(props: {
203+
scope: cdk.Construct;
204+
cidr: string;
205+
routeName: string;
206+
transitGateway: TransitGatewayOutput;
207+
attachmentId?: string;
208+
blackhole?: boolean;
209+
}) {
210+
const { cidr, routeName, scope, transitGateway, attachmentId, blackhole } = props;
188211
const routesMap = transitGateway.tgwRouteTableNameToIdMap;
189212
if (routeName === '{TGW_ALL}') {
190213
for (const key of Object.keys(routesMap)) {
191-
CreateTransitGatewayRoute(scope, key, attachmentId, routesMap[key], cidr, blackhole);
214+
CreateTransitGatewayRoute({
215+
scope,
216+
name: key,
217+
routeId: routesMap[key],
218+
cidrBlock: cidr,
219+
blackhole,
220+
attachmentId,
221+
});
192222
}
193223
} else {
194224
const routeId = routesMap[routeName];
195-
CreateTransitGatewayRoute(scope, routeName, attachmentId, routeId, cidr, blackhole);
225+
CreateTransitGatewayRoute({
226+
scope,
227+
name: routeName,
228+
routeId,
229+
cidrBlock: cidr,
230+
attachmentId,
231+
blackhole,
232+
});
196233
}
197234
}
198235

@@ -221,14 +258,15 @@ function CreateAssociations(
221258
}
222259
}
223260

224-
function CreateTransitGatewayRoute(
225-
scope: cdk.Construct,
226-
name: string,
227-
attachmentId: string,
228-
routeId: string,
229-
cidrBlock: string,
230-
blackhole?: boolean,
231-
) {
261+
function CreateTransitGatewayRoute(props: {
262+
scope: cdk.Construct;
263+
name: string;
264+
routeId: string;
265+
cidrBlock: string;
266+
attachmentId?: string;
267+
blackhole?: boolean;
268+
}) {
269+
const { attachmentId, cidrBlock, name, routeId, scope, blackhole } = props;
232270
// TODO need to update the id by calculating the hash of the properties
233271
const id = `${name}${attachmentId}${routeId}${cidrBlock}${blackhole}`;
234272
if (!blackhole) {

src/lib/custom-resources/cdk-guardduty-admin-setup/runtime/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async function createMembers(memberAccounts: AccountDetail[], detectorId: string
102102
let currentAccounts: AccountDetail[] = paginate(memberAccounts, pageNumber, pageSize);
103103
while (currentAccounts.length > 0) {
104104
console.log(`Calling api "guardduty.createMembers()", ${currentAccounts}, ${detectorId}`);
105-
await throttlingBackOff(() =>
105+
const createMembersResp = await throttlingBackOff(() =>
106106
guardduty
107107
.createMembers({
108108
AccountDetails: currentAccounts,
@@ -111,6 +111,7 @@ async function createMembers(memberAccounts: AccountDetail[], detectorId: string
111111
.promise(),
112112
);
113113
currentAccounts = paginate(memberAccounts, ++pageNumber, pageSize);
114+
console.log(`UnProcessedAccounts are : ${JSON.stringify(createMembersResp.UnprocessedAccounts)}`);
114115
}
115116
} catch (error) {
116117
console.error(

0 commit comments

Comments
 (0)