Skip to content

Commit aa73534

Browse files
authored
fix(ec2-alpha): fix resource id references and tags for migration behind feature flag (#34377)
### Issue # (if applicable) Closes NA. ### Reason for this change Align resource ids and tag changes in Subnetv2 and VPCv2 constructs to allow a migration path for customers. ### Description of changes - Add a new feature flag to keep the resource reference same as VPCv1 and prevent replacement of resources. - Change id references from `Get::Att` to `Ref` for VPC, RouteTargetId, NatGW, IGW and RouteTable. - Align subnet and IGW tag. ### Describe any new or updated permissions being added NA ### Description of how you validated changes Added unit test and integration test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e493cc8 commit aa73534

File tree

95 files changed

+1481
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1481
-234
lines changed

packages/@aws-cdk/aws-ec2-alpha/lib/route.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { CfnEIP, CfnEgressOnlyInternetGateway, CfnInternetGateway, CfnNatGateway, CfnVPCPeeringConnection, CfnRoute, CfnRouteTable, CfnVPCGatewayAttachment, CfnVPNGateway, CfnVPNGatewayRoutePropagation, GatewayVpcEndpoint, IRouteTable, IVpcEndpoint, RouterType } from 'aws-cdk-lib/aws-ec2';
22
import { Construct, IDependable } from 'constructs';
3-
import { Annotations, Duration, IResource, Resource, Tags, ValidationError } from 'aws-cdk-lib/core';
3+
import { Annotations, Duration, FeatureFlags, IResource, Resource, Tags, ValidationError } from 'aws-cdk-lib/core';
44
import { IVpcV2, VPNGatewayV2Options } from './vpc-v2-base';
55
import { NetworkUtils, allRouteTableIds, CidrBlock } from './util';
66
import { ISubnetV2 } from './subnet-v2';
77
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
8+
import { cx_api } from 'aws-cdk-lib';
89

910
/**
1011
* Indicates whether the NAT gateway supports public or private connectivity.
@@ -289,13 +290,18 @@ export class InternetGateway extends Resource implements IRouteTarget {
289290
this.resource = new CfnInternetGateway(this, 'IGW', {});
290291
this.node.defaultChild = this.resource;
291292

292-
this.routerTargetId = this.resource.attrInternetGatewayId;
293+
this.routerTargetId = FeatureFlags.of(this).isEnabled(cx_api.USE_RESOURCEID_FOR_VPCV2_MIGRATION) ?
294+
this.resource.ref : this.resource.attrInternetGatewayId;
293295
this.vpcId = props.vpc.vpcId;
294296

295297
if (props.internetGatewayName) {
296298
Tags.of(this).add(NAME_TAG, props.internetGatewayName);
297299
}
298300

301+
if (props.vpc.vpcName) {
302+
Tags.of(this).add('Name', props.vpc.vpcName);
303+
}
304+
299305
new CfnVPCGatewayAttachment(this, 'GWAttachment', {
300306
vpcId: this.vpcId,
301307
internetGatewayId: this.routerTargetId,
@@ -426,6 +432,11 @@ export class NatGateway extends Resource implements IRouteTarget {
426432
*/
427433
public readonly resource: CfnNatGateway;
428434

435+
/**
436+
* Elastic IP created for allocation
437+
*/
438+
public readonly eip?: CfnEIP;
439+
429440
constructor(scope: Construct, id: string, props: NatGatewayProps) {
430441
super(scope, id);
431442
// Enhanced CDK Analytics Telemetry
@@ -450,10 +461,10 @@ export class NatGateway extends Resource implements IRouteTarget {
450461
var aId: string | undefined;
451462
if (this.connectivityType === NatConnectivityType.PUBLIC) {
452463
if (!props.allocationId) {
453-
let eip = new CfnEIP(this, 'EIP', {
464+
this.eip = new CfnEIP(this, 'EIP', {
454465
domain: 'vpc',
455466
});
456-
aId = eip.attrAllocationId;
467+
aId = this.eip.attrAllocationId;
457468
} else {
458469
aId = props.allocationId;
459470
}
@@ -466,11 +477,14 @@ export class NatGateway extends Resource implements IRouteTarget {
466477
secondaryAllocationIds: props.secondaryAllocationIds,
467478
...props,
468479
});
469-
this.natGatewayId = this.resource.attrNatGatewayId;
480+
this.natGatewayId = FeatureFlags.of(this).isEnabled(cx_api.USE_RESOURCEID_FOR_VPCV2_MIGRATION) ?
481+
this.resource.ref : this.resource.attrNatGatewayId;
482+
483+
this.routerTargetId = FeatureFlags.of(this).isEnabled(cx_api.USE_RESOURCEID_FOR_VPCV2_MIGRATION) ?
484+
this.resource.ref : this.resource.attrNatGatewayId;
470485

471-
this.routerTargetId = this.resource.attrNatGatewayId;
472486
this.node.defaultChild = this.resource;
473-
this.node.addDependency(props.subnet.internetConnectivityEstablished);
487+
this.resource.node.addDependency(props.subnet.internetConnectivityEstablished);
474488
}
475489
}
476490

@@ -809,7 +823,8 @@ export class RouteTable extends Resource implements IRouteTable {
809823
}
810824
this.node.defaultChild = this.resource;
811825

812-
this.routeTableId = this.resource.attrRouteTableId;
826+
this.routeTableId = FeatureFlags.of(this).isEnabled(cx_api.USE_RESOURCEID_FOR_VPCV2_MIGRATION) ?
827+
this.resource.ref : this.resource.attrRouteTableId;
813828
}
814829

815830
/**

packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Resource, Names, Lazy, Tags, Token, ValidationError, UnscopedValidation
22
import { CfnSubnet, CfnSubnetRouteTableAssociation, INetworkAcl, IRouteTable, ISubnet, NetworkAcl, SubnetNetworkAclAssociation, SubnetType } from 'aws-cdk-lib/aws-ec2';
33
import { Construct, DependencyGroup, IDependable } from 'constructs';
44
import { IVpcV2 } from './vpc-v2-base';
5-
import { CidrBlock, CidrBlockIpv6 } from './util';
5+
import { CidrBlock, CidrBlockIpv6, defaultSubnetName } from './util';
66
import { RouteTable } from './route';
77
import { addConstructMetadata, MethodMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
88

@@ -28,14 +28,14 @@ export class IpCidr implements ICidr {
2828
}
2929

3030
/**
31-
* Name tag constant
31+
* VPC Name tag constant
3232
*/
33-
const NAME_TAG: string = 'Name';
33+
const SUBNETTYPE_TAG = 'aws-cdk:subnet-type';
3434

3535
/**
36-
* VPC Name tag constant
36+
* Subnet Name tag constant
3737
*/
38-
const VPCNAME_TAG: string = 'VpcName';
38+
const SUBNETNAME_TAG = 'aws-cdk:subnet-name';
3939

4040
/**
4141
* Properties to define subnet for VPC.
@@ -71,6 +71,13 @@ export interface SubnetV2Props {
7171
*/
7272
readonly routeTable?: IRouteTable;
7373

74+
/**
75+
* Name of the default RouteTable created by CDK to be used for tagging
76+
*
77+
* @default - default route table name created by CDK as 'DefaultCDKRouteTable'
78+
*/
79+
readonly defaultRouteTableName ?: string;
80+
7481
/**
7582
* The type of Subnet to configure.
7683
*
@@ -307,21 +314,20 @@ export class SubnetV2 extends Resource implements ISubnetV2 {
307314

308315
this._networkAcl = NetworkAcl.fromNetworkAclId(this, 'Acl', subnet.attrNetworkAclAssociationId);
309316

317+
const includeResourceTypes = [CfnSubnet.CFN_RESOURCE_TYPE_NAME];
310318
if (props.subnetName) {
311-
Tags.of(this).add(NAME_TAG, props.subnetName);
312-
}
313-
314-
if (props.vpc.vpcName) {
315-
Tags.of(this).add(VPCNAME_TAG, props.vpc.vpcName);
319+
Tags.of(subnet).add(SUBNETNAME_TAG, props.subnetName);
316320
}
321+
const subnetTypeName = defaultSubnetName(props.subnetType) ?? 'undefined';
322+
Tags.of(subnet).add(SUBNETTYPE_TAG, subnetTypeName, { includeResourceTypes });
317323

318324
if (props.routeTable) {
319325
this._routeTable = props.routeTable;
320326
} else {
321327
// Assigning a default route table
322328
this._routeTable = new RouteTable(this, 'RouteTable', {
323329
vpc: props.vpc,
324-
routeTableName: 'DefaultCDKRouteTable',
330+
routeTableName: props.defaultRouteTableName ?? 'DefaultCDKRouteTable',
325331
});
326332
}
327333

packages/@aws-cdk/aws-ec2-alpha/lib/util.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
/* eslint no-bitwise: ["error", { "allow": ["~", "|", "<<", "&"] }] */
22

3-
import { ISubnet } from 'aws-cdk-lib/aws-ec2';
3+
import { ISubnet, SubnetType } from 'aws-cdk-lib/aws-ec2';
4+
5+
/**
6+
* The default names for every subnet type
7+
*/
8+
export function defaultSubnetName(type: SubnetType) {
9+
switch (type) {
10+
case SubnetType.PUBLIC: return 'Public';
11+
case SubnetType.PRIVATE_WITH_NAT:
12+
case SubnetType.PRIVATE_WITH_EGRESS:
13+
return 'Private';
14+
case SubnetType.PRIVATE_ISOLATED:
15+
return 'Isolated';
16+
}
17+
return undefined;
18+
}
419

520
/**
621
* Return a subnet name from its construct ID

packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CfnVPC, CfnVPCCidrBlock, DefaultInstanceTenancy, ISubnet, SubnetType } from 'aws-cdk-lib/aws-ec2';
2-
import { Arn, CfnResource, Lazy, Names, Resource, Tags } from 'aws-cdk-lib/core';
2+
import { Arn, CfnResource, FeatureFlags, Lazy, Names, Resource, Tags } from 'aws-cdk-lib/core';
33
import { Construct, DependencyGroup, IDependable } from 'constructs';
44
import { IpamOptions, IIpamPool } from './ipam';
55
import { IVpcV2, VpcV2Base } from './vpc-v2-base';
66
import { ISubnetV2, SubnetV2, SubnetV2Attributes } from './subnet-v2';
7-
import { region_info } from 'aws-cdk-lib';
7+
import { cx_api, region_info } from 'aws-cdk-lib';
88
import { addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
99

1010
/**
@@ -521,7 +521,8 @@ export class VpcV2 extends VpcV2Base {
521521
this.ipv4CidrBlock = vpcOptions.ipv4CidrBlock;
522522
}
523523
this.ipv6CidrBlocks = this.resource.attrIpv6CidrBlocks;
524-
this.vpcId = this.resource.attrVpcId;
524+
this.vpcId = FeatureFlags.of(this).isEnabled(cx_api.USE_RESOURCEID_FOR_VPCV2_MIGRATION) ?
525+
this.resource.ref : this.resource.attrVpcId;
525526
this.vpcArn = Arn.format({
526527
service: 'ec2',
527528
resource: 'vpc',

packages/@aws-cdk/aws-ec2-alpha/test/integ.byoip-ipv6.js.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)