Skip to content

fix(ec2): dual-stack vpc without private subnets creates EgressOnlyInternetGateway (under feature flag) #34437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

maharajhaider
Copy link
Contributor

@maharajhaider maharajhaider commented May 12, 2025

Issue # (if applicable)

Closes #30981.

Reason for this change

-> EgressOnlyInternetGateway was been created even without any private subnets

Description of changes

-> Fixed the condition that determins if a EgressOnlyInternetGateway will be created
-> Added feature flag

Describe any new or updated permissions being added

N/A

Description of how you validated changes

I added two new unit tests that checks if EgressOnlyInternetGateway is created without a private subnet

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels May 12, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team May 12, 2025 16:39
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label May 12, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@maharajhaider maharajhaider changed the title Fix ec2 dual stack redundant egress [Fix] Condition fixed to prevent redundant EgressOnlyInternetGateway creation May 12, 2025
@maharajhaider maharajhaider changed the title [Fix] Condition fixed to prevent redundant EgressOnlyInternetGateway creation fix (ec2): Condition fixed to prevent redundant EgressOnlyInternetGateway creation May 12, 2025
@maharajhaider maharajhaider force-pushed the fix-ec2-dual-stack-redundant-egress branch from 82ac2e3 to a0d833f Compare May 12, 2025 17:36
@paulhcsun paulhcsun self-assigned this May 12, 2025
@maharajhaider maharajhaider force-pushed the fix-ec2-dual-stack-redundant-egress branch from 7332c64 to 427b4a2 Compare May 12, 2025 22:56
@maharajhaider maharajhaider changed the title fix (ec2): Condition fixed to prevent redundant EgressOnlyInternetGateway creation fix (ec2): Condition fixed to prevent redundant EgressOnlyInternetGateway creation(under feature flag) May 12, 2025
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: d157dce
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@paulhcsun paulhcsun changed the title fix (ec2): Condition fixed to prevent redundant EgressOnlyInternetGateway creation(under feature flag) fix(ec2): condition fixed to prevent redundant EgressOnlyInternetGateway creation(under feature flag) May 13, 2025
@paulhcsun paulhcsun changed the title fix(ec2): condition fixed to prevent redundant EgressOnlyInternetGateway creation(under feature flag) fix(ec2): condition fixed to prevent redundant EgressOnlyInternetGateway creation (under feature flag) May 13, 2025
@paulhcsun
Copy link
Contributor

For the PR title please change it to describe the bug and not the actual fix.

@maharajhaider maharajhaider changed the title fix(ec2): condition fixed to prevent redundant EgressOnlyInternetGateway creation (under feature flag) fix(ec2): dual-stack vpc without private subnets creates EgressOnlyInternetGateway (under feature flag) May 13, 2025
Copy link
Contributor

@paulhcsun paulhcsun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work with this change! Just a few comments regarding the naming of the feature flag.

@@ -137,6 +137,7 @@ export const DYNAMODB_TABLE_RETAIN_TABLE_REPLICA = '@aws-cdk/aws-dynamodb:retain
export const LOG_USER_POOL_CLIENT_SECRET_VALUE = '@aws-cdk/cognito:logUserPoolClientSecretValue';
export const PIPELINE_REDUCE_CROSS_ACCOUNT_ACTION_ROLE_TRUST_SCOPE = '@aws-cdk/pipelines:reduceCrossAccountActionRoleTrustScope';
export const S3_TRUST_KEY_POLICY_FOR_SNS_SUBSCRIPTIONS = '@aws-cdk/s3-notifications:addS3TrustKeyPolicyForSnsSubscriptions';
export const ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC = '@aws-cdk/ec2:removeEgressOnlyGatewayFromPublicSubnetVPC';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I know it's not consistent but the convention is to prefix the service here with aws-. Also we should prefix the constant with the module name EC2- and update the name of the feature flag to more accurately reflect the behaviour.

Suggested change
export const ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC = '@aws-cdk/ec2:removeEgressOnlyGatewayFromPublicSubnetVPC';
export const EC2_REQUIRE_PRIVATE_SUBNETS_FOR_EGRESSONLYINTERNETGATEWAY = '@aws-cdk/aws-ec2:requirePrivateSubnetsForEgressOnlyInternetGateway';

@@ -1576,6 +1577,17 @@ export const FLAGS: Record<string, FlagInfo> = {
},

//////////////////////////////////////////////////////////////////////
[ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC]: {
type: FlagType.BugFix,
summary: 'Remove EgressOnlyGateway resource when a a double stack vpc has only public subnets',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem entirely correct. Rather than "removing EgressOnlyGateway when a stack only has public subnets", a more accurate summary could be "When enabled, the EgressOnlyGateway resource is only created if private subnets are defined in the VPC."

@@ -1649,7 +1649,13 @@ export class Vpc extends VpcBase {
}

// Create an Egress Only Internet Gateway and attach it if necessary
if (this.useIpv6 && this.privateSubnets) {

const redundantEgressOnlyGatewayRemovalFeatureFlag =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: variable name

Suggested change
const redundantEgressOnlyGatewayRemovalFeatureFlag =
const isRequirePrivateSubnetsForEgressOnlyIgw =

@@ -2747,6 +2747,90 @@ describe('vpc', () => {
},
});
});
test('(default)EgressOnlyInternetGateWay is created when no private subnet configured in dual stack', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of (default) EgressOnlyIGW.... maybe change this to say "EgressOnlyIGW is created when....in dual stack when feature flag is enabled."

// THEN
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
});
test('(default)EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above test.

Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
});

test('(feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
test('(feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is created when private subnet configured in dual stack', () => {
test('EgressOnlyInternetGateWay is created when private subnet configured in dual stack when feature flag is enabled', () => {

// THEN
Template.fromStack(stack).resourceCountIs('AWS::EC2::EgressOnlyInternetGateway', 1);
});
test(' (feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is not created when private subnet configured in dual stack', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test(' (feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is not created when private subnet configured in dual stack', () => {
test(' (feature flag ENABLE_E2_REMOVE_EGRESSONLYGATEWAY_FROM_PUBLIC_SUBNET_VPC)EgressOnlyInternetGateWay is not created when no private subnets are configured in dual stack', () => {

@paulhcsun paulhcsun added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label May 13, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review May 13, 2025 22:13

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-ec2): Vpc creates EgressOnlyInternetGateway without private subnets
3 participants