@@ -4,6 +4,7 @@ import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
44import * as targets from 'aws-cdk-lib/aws-elasticloadbalancingv2-targets' ;
55import { AwsCustomResource , AwsCustomResourcePolicy , PhysicalResourceId } from 'aws-cdk-lib/custom-resources' ;
66import { Construct } from 'constructs' ;
7+ import { RouteTableManager } from './routeTableManager' ;
78import { ObjToStrMap } from '../utils/common' ;
89export interface NetworkACL {
910 readonly cidr : ec2 . AclCidr ;
@@ -61,6 +62,7 @@ export interface ISubnetsProps {
6162 readonly routes ?: AddRouteOptions [ ] ;
6263 readonly tags ?: Record < string , string > ;
6364 readonly useSubnetForNAT ?: boolean ;
65+ readonly isMigration ?: boolean ;
6466}
6567export interface VPCProps {
6668 readonly vpc : ec2 . VpcProps ;
@@ -256,6 +258,14 @@ export class Network extends Construct {
256258 ) ;
257259 }
258260
261+ // Create a single RouteTableManager for the entire subnet group if migration is enabled
262+ const routeTableManager = option . isMigration ? new RouteTableManager ( this , `${ option . subnetGroupName } RouteTableManager` , {
263+ vpc,
264+ subnetGroupName : option . subnetGroupName ,
265+ routes : option . routes ,
266+ peeringConnectionId,
267+ } ) : undefined ;
268+
259269 option . availabilityZones . forEach ( ( az , index ) => {
260270 let subnet : ec2 . PrivateSubnet | ec2 . PublicSubnet =
261271 option . subnetType === ec2 . SubnetType . PUBLIC
@@ -267,7 +277,6 @@ export class Network extends Construct {
267277 cidrBlock : option . cidrBlock [ index ] ,
268278 vpcId : vpc . vpcId ,
269279 mapPublicIpOnLaunch : true ,
270-
271280 } ,
272281 )
273282 : new ec2 . PrivateSubnet (
@@ -280,31 +289,35 @@ export class Network extends Construct {
280289 mapPublicIpOnLaunch : false ,
281290 } ,
282291 ) ;
283- option . routes ?. forEach ( ( route , routeIndex ) => {
284- if ( peeringConnectionId != undefined && route . existingVpcPeeringRouteKey != undefined ) {
285- let routeId : ec2 . CfnVPCPeeringConnection | undefined = peeringConnectionId [ route . existingVpcPeeringRouteKey ] ;
286- if ( routeId != undefined ) {
292+ if ( option . isMigration && routeTableManager ) {
293+ routeTableManager . associateSubnet ( subnet , index ) ;
294+ } else {
295+ option . routes ?. forEach ( ( route , routeIndex ) => {
296+ if ( peeringConnectionId != undefined && route . existingVpcPeeringRouteKey != undefined ) {
297+ let routeId : ec2 . CfnVPCPeeringConnection | undefined = peeringConnectionId [ route . existingVpcPeeringRouteKey ] ;
298+ if ( routeId != undefined ) {
299+ subnet . addRoute (
300+ `${ option . subnetGroupName } ${ routeIndex } RouteEntry` ,
301+ {
302+ routerId : routeId . ref ,
303+ routerType : route . routerType ,
304+ destinationCidrBlock : route . destinationCidrBlock ,
305+ } ,
306+ ) ;
307+ }
308+ } else if ( route . routerId != undefined ) {
287309 subnet . addRoute (
288310 `${ option . subnetGroupName } ${ routeIndex } RouteEntry` ,
289311 {
290- routerId : routeId . ref ,
312+ routerId : route . routerId ?? '' ,
291313 routerType : route . routerType ,
292314 destinationCidrBlock : route . destinationCidrBlock ,
293315 } ,
294316 ) ;
295317 }
296- } else if ( route . routerId != undefined ) {
297- subnet . addRoute (
298- `${ option . subnetGroupName } ${ routeIndex } RouteEntry` ,
299- {
300- routerId : route . routerId ?? '' ,
301- routerType : route . routerType ,
302- destinationCidrBlock : route . destinationCidrBlock ,
303- } ,
304- ) ;
305- }
318+ } ) ;
319+ }
306320
307- } ) ;
308321 Tags . of ( subnet ) . add ( SUBNETNAME_TAG , option . subnetGroupName ) ;
309322 Tags . of ( subnet ) . add ( SUBNETTYPE_TAG , option . subnetType ) ;
310323 if ( option . tags != undefined ) {
0 commit comments