Skip to content

Commit 6b8d59f

Browse files
author
Maharaj Haider
committed
add feature flag to vpc dual stack change
1 parent e1d3f5a commit 6b8d59f

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

packages/aws-cdk-lib/aws-ec2/lib/vpc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,13 @@ export class Vpc extends VpcBase {
16491649
}
16501650

16511651
// Create an Egress Only Internet Gateway and attach it if necessary
1652-
if (this.useIpv6 && this.privateSubnets.length>0) {
1652+
1653+
const redundantEgressOnlyGatewayRemovalFeatureFlag =
1654+
FeatureFlags.of(this).isEnabled(cxapi.ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC);
1655+
1656+
if ((this.useIpv6 && !redundantEgressOnlyGatewayRemovalFeatureFlag && this.privateSubnets) ||
1657+
(this.useIpv6 && redundantEgressOnlyGatewayRemovalFeatureFlag && this.privateSubnets.length > 0)
1658+
) {
16531659
const eigw = new CfnEgressOnlyInternetGateway(this, 'EIGW6', {
16541660
vpcId: this.vpcId,
16551661
});

packages/aws-cdk-lib/aws-ec2/test/vpc.test.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
22
import { Annotations, Match, Template } from '../../assertions';
33
import { App, CfnOutput, CfnResource, Fn, Lazy, Stack, Tags } from '../../core';
4-
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP } from '../../cx-api';
4+
import { EC2_RESTRICT_DEFAULT_SECURITY_GROUP, ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC } from '../../cx-api';
55
import {
66
AclCidr,
77
AclTraffic,
@@ -2747,8 +2747,7 @@ describe('vpc', () => {
27472747
},
27482748
});
27492749
});
2750-
2751-
test('EgressOnlyInternetGateWay is not created when no private subnet configured in dual stack', () => {
2750+
test('(default)EgressOnlyInternetGateWay is created when no private subnet configured in dual stack', () => {
27522751
// GIVEN
27532752
const app = new App();
27542753
const stack = new Stack(app, 'DualStackStack');
@@ -2765,9 +2764,9 @@ describe('vpc', () => {
27652764
});
27662765

27672766
// THEN
2768-
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 0);
2767+
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
27692768
});
2770-
test('EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
2769+
test('(default)EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
27712770
// GIVEN
27722771
const app = new App();
27732772
const stack = new Stack(app, 'DualStackStack');
@@ -2791,6 +2790,50 @@ describe('vpc', () => {
27912790
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
27922791
});
27932792

2793+
test('(feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
2794+
// GIVEN
2795+
const app = new App();
2796+
const stack = new Stack(app, 'DualStackStack');
2797+
// WHEN
2798+
stack.node.setContext(ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC, true);
2799+
const vpc = new Vpc(stack, 'Vpc', {
2800+
ipProtocol: IpProtocol.DUAL_STACK,
2801+
subnetConfiguration: [
2802+
{
2803+
subnetType: SubnetType.PUBLIC,
2804+
name: 'public',
2805+
},
2806+
{
2807+
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
2808+
name: 'private',
2809+
},
2810+
],
2811+
});
2812+
2813+
// THEN
2814+
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
2815+
});
2816+
test(' (feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is not created when private subnet configured in dual stack', () => {
2817+
// GIVEN
2818+
const app = new App();
2819+
const stack = new Stack(app, 'DualStackStack');
2820+
2821+
2822+
// WHEN
2823+
stack.node.setContext(ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC, true);
2824+
const vpc = new Vpc(stack, 'Vpc', {
2825+
ipProtocol: IpProtocol.DUAL_STACK,
2826+
subnetConfiguration: [
2827+
{
2828+
subnetType: SubnetType.PUBLIC,
2829+
name: 'public',
2830+
},
2831+
],
2832+
});
2833+
// THEN
2834+
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 0);
2835+
});
2836+
27942837
test('error should occur if IPv6 properties are provided for a non-dual-stack VPC', () => {
27952838
// GIVEN
27962839
const app = new App();

packages/aws-cdk-lib/cx-api/lib/features.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const DYNAMODB_TABLE_RETAIN_TABLE_REPLICA = '@aws-cdk/aws-dynamodb:retain
137137
export const LOG_USER_POOL_CLIENT_SECRET_VALUE = '@aws-cdk/cognito:logUserPoolClientSecretValue';
138138
export const PIPELINE_REDUCE_CROSS_ACCOUNT_ACTION_ROLE_TRUST_SCOPE = '@aws-cdk/pipelines:reduceCrossAccountActionRoleTrustScope';
139139
export const S3_TRUST_KEY_POLICY_FOR_SNS_SUBSCRIPTIONS = '@aws-cdk/s3-notifications:addS3TrustKeyPolicyForSnsSubscriptions';
140+
export const ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC = '@aws-cdk/ec2:removeEgressOnlyGatewayFromPublicSubnetVPC';
140141

141142
export const FLAGS: Record<string, FlagInfo> = {
142143
//////////////////////////////////////////////////////////////////////
@@ -1573,6 +1574,18 @@ export const FLAGS: Record<string, FlagInfo> = {
15731574
introducedIn: { v2: '2.195.0' },
15741575
recommendedValue: true,
15751576
},
1577+
1578+
//////////////////////////////////////////////////////////////////////
1579+
[ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC]: {
1580+
type: FlagType.BugFix,
1581+
summary: 'Remove EgressOnlyGateway resource when a a double stack vpc has only public subnets',
1582+
detailsMd: `
1583+
When this feature flag is enabled, EgressOnlyGateway resource will not be created when you create a vpc with only public subnets. A
1584+
`,
1585+
introducedIn: { v2: '2.196.0' },
1586+
defaults: { v2: false },
1587+
recommendedValue: true,
1588+
},
15761589
};
15771590

15781591
const CURRENT_MV = 'v2';

0 commit comments

Comments
 (0)