11import { CfnEIP , CfnEgressOnlyInternetGateway , CfnInternetGateway , CfnNatGateway , CfnVPCPeeringConnection , CfnRoute , CfnRouteTable , CfnVPCGatewayAttachment , CfnVPNGateway , CfnVPNGatewayRoutePropagation , GatewayVpcEndpoint , IRouteTable , IVpcEndpoint , RouterType } from 'aws-cdk-lib/aws-ec2' ;
22import { Construct , IDependable } from 'constructs' ;
3- import { Annotations , Duration , IResource , Resource } from 'aws-cdk-lib/core' ;
3+ import { Annotations , Duration , IResource , Resource , Tags } from 'aws-cdk-lib/core' ;
44import { IVpcV2 , VPNGatewayV2Options } from './vpc-v2-base' ;
55import { NetworkUtils , allRouteTableIds , CidrBlock } from './util' ;
66import { ISubnetV2 } from './subnet-v2' ;
@@ -209,6 +209,11 @@ export interface VPCPeeringConnectionProps extends VPCPeeringConnectionOptions {
209209 readonly requestorVpc : IVpcV2 ;
210210}
211211
212+ /**
213+ * Name tag constant
214+ */
215+ const NAME_TAG : string = 'Name' ;
216+
212217/**
213218 * Creates an egress-only internet gateway
214219 * @resource AWS::EC2::EgressOnlyInternetGateway
@@ -232,6 +237,9 @@ export class EgressOnlyInternetGateway extends Resource implements IRouteTarget
232237 constructor ( scope : Construct , id : string , props : EgressOnlyInternetGatewayProps ) {
233238 super ( scope , id ) ;
234239
240+ if ( props . egressOnlyInternetGatewayName ) {
241+ Tags . of ( this ) . add ( NAME_TAG , props . egressOnlyInternetGatewayName ) ;
242+ }
235243 this . routerType = RouterType . EGRESS_ONLY_INTERNET_GATEWAY ;
236244
237245 this . resource = new CfnEgressOnlyInternetGateway ( this , 'EIGW' , {
@@ -279,6 +287,10 @@ export class InternetGateway extends Resource implements IRouteTarget {
279287 this . routerTargetId = this . resource . attrInternetGatewayId ;
280288 this . vpcId = props . vpc . vpcId ;
281289
290+ if ( props . internetGatewayName ) {
291+ Tags . of ( this ) . add ( NAME_TAG , props . internetGatewayName ) ;
292+ }
293+
282294 new CfnVPCGatewayAttachment ( this , 'GWAttachment' , {
283295 vpcId : this . vpcId ,
284296 internetGatewayId : this . routerTargetId ,
@@ -322,7 +334,9 @@ export class VPNGatewayV2 extends Resource implements IRouteTarget {
322334 private readonly _routePropagation : CfnVPNGatewayRoutePropagation ;
323335
324336 constructor ( scope : Construct , id : string , props : VPNGatewayV2Props ) {
325- super ( scope , id ) ;
337+ super ( scope , id , {
338+ physicalName : props . vpnGatewayName ,
339+ } ) ;
326340
327341 this . routerType = RouterType . GATEWAY ;
328342
@@ -340,6 +354,10 @@ export class VPNGatewayV2 extends Resource implements IRouteTarget {
340354 vpnGatewayId : this . resource . attrVpnGatewayId ,
341355 } ) ;
342356
357+ if ( props . vpnGatewayName ) {
358+ Tags . of ( this ) . add ( NAME_TAG , props . vpnGatewayName ) ;
359+ }
360+
343361 // Propagate routes on route tables associated with the right subnets
344362 const vpnRoutePropagation = props . vpnRoutePropagation ?? [ ] ;
345363 const subnets = vpnRoutePropagation . map ( s => props . vpc . selectSubnets ( s ) . subnets ) . flat ( ) ;
@@ -365,15 +383,22 @@ export class VPNGatewayV2 extends Resource implements IRouteTarget {
365383 * @resource AWS::EC2::NatGateway
366384 */
367385export class NatGateway extends Resource implements IRouteTarget {
386+
387+ /**
388+ * Id of the NatGateway
389+ * @attribute
390+ */
391+ public readonly natGatewayId : string ;
392+
368393 /**
369394 * The type of router used in the route.
370395 */
371- readonly routerType : RouterType ;
396+ public readonly routerType : RouterType ;
372397
373398 /**
374399 * The ID of the route target.
375400 */
376- readonly routerTargetId : string ;
401+ public readonly routerTargetId : string ;
377402
378403 /**
379404 * Indicates whether the NAT gateway supports public or private connectivity.
@@ -409,6 +434,10 @@ export class NatGateway extends Resource implements IRouteTarget {
409434 }
410435 }
411436
437+ if ( props . natGatewayName ) {
438+ Tags . of ( this ) . add ( NAME_TAG , props ?. natGatewayName ) ;
439+ }
440+
412441 // If user does not provide EIP, generate one for them
413442 var aId : string | undefined ;
414443 if ( this . connectivityType === NatConnectivityType . PUBLIC ) {
@@ -429,6 +458,7 @@ export class NatGateway extends Resource implements IRouteTarget {
429458 secondaryAllocationIds : props . secondaryAllocationIds ,
430459 ...props ,
431460 } ) ;
461+ this . natGatewayId = this . resource . attrNatGatewayId ;
432462
433463 this . routerTargetId = this . resource . attrNatGatewayId ;
434464 this . node . defaultChild = this . resource ;
@@ -476,6 +506,9 @@ export class VPCPeeringConnection extends Resource implements IRouteTarget {
476506 if ( overlap ) {
477507 throw new Error ( 'CIDR block should not overlap with each other for establishing a peering connection' ) ;
478508 }
509+ if ( props . vpcPeeringConnectionName ) {
510+ Tags . of ( this ) . add ( NAME_TAG , props . vpcPeeringConnectionName ) ;
511+ }
479512
480513 this . resource = new CfnVPCPeeringConnection ( this , 'VPCPeeringConnection' , {
481514 vpcId : props . requestorVpc . vpcId ,
@@ -700,7 +733,6 @@ export class Route extends Resource implements IRouteV2 {
700733 throw new Error ( 'Egress only internet gateway does not support IPv4 routing' ) ;
701734 }
702735 this . targetRouterType = this . target . gateway ? this . target . gateway . routerType : RouterType . VPC_ENDPOINT ;
703-
704736 // Gateway generates route automatically via its RouteTable, thus we don't need to generate the resource for it
705737 if ( ! ( this . target . endpoint instanceof GatewayVpcEndpoint ) ) {
706738 this . resource = new CfnRoute ( this , 'Route' , {
@@ -761,6 +793,9 @@ export class RouteTable extends Resource implements IRouteTable {
761793 this . resource = new CfnRouteTable ( this , 'RouteTable' , {
762794 vpcId : props . vpc . vpcId ,
763795 } ) ;
796+ if ( props . routeTableName ) {
797+ Tags . of ( this ) . add ( NAME_TAG , props . routeTableName ) ;
798+ }
764799 this . node . defaultChild = this . resource ;
765800
766801 this . routeTableId = this . resource . attrRouteTableId ;
@@ -771,12 +806,14 @@ export class RouteTable extends Resource implements IRouteTable {
771806 *
772807 * @param destination The IPv4 or IPv6 CIDR block used for the destination match.
773808 * @param target The gateway or endpoint targeted by the route.
809+ * @param routeName The resource name of the route.
774810 */
775- public addRoute ( id : string , destination : string , target : RouteTargetType ) {
811+ public addRoute ( id : string , destination : string , target : RouteTargetType , routeName ?: string ) {
776812 new Route ( this , id , {
777813 routeTable : this ,
778814 destination : destination ,
779815 target : target ,
816+ routeName : routeName ,
780817 } ) ;
781818 }
782819}
0 commit comments