Skip to content

Commit

Permalink
fix(ec2): internet gateway is created even if public subnets are rese…
Browse files Browse the repository at this point in the history
…rved
  • Loading branch information
go-to-k committed Jan 7, 2024
1 parent a8a639e commit 40ac987
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ export class Vpc extends VpcBase {

const createInternetGateway = props.createInternetGateway ?? true;
const allowOutbound = this.subnetConfiguration.filter(
subnet => (subnet.subnetType !== SubnetType.PRIVATE_ISOLATED && subnet.subnetType !== SubnetType.ISOLATED)).length > 0;
subnet => (subnet.subnetType !== SubnetType.PRIVATE_ISOLATED && subnet.subnetType !== SubnetType.ISOLATED && !subnet.reserved)).length > 0;

// Create an Internet Gateway and attach it if necessary
if (allowOutbound && createInternetGateway) {
Expand Down
20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/vpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,26 @@ describe('vpc', () => {

});

test('with reserved public subnets, should not create the internet gateway', () => {
const stack = getTestStack();
const vpc = new Vpc(stack, 'TheVPC', {
subnetConfiguration: [
{
subnetType: SubnetType.PRIVATE_ISOLATED,
name: 'isolated',
},
{
subnetType: SubnetType.PUBLIC,
name: 'public',
reserved: true,
},
],
});
Template.fromStack(stack).resourceCountIs('AWS::EC2::InternetGateway', 0);
Template.fromStack(stack).resourceCountIs('AWS::EC2::VPCGatewayAttachment', 0);

});

test('with subnets and reserved subnets defined, VPC subnet count should not contain reserved subnets ', () => {
const stack = getTestStack();
new Vpc(stack, 'TheVPC', {
Expand Down

0 comments on commit 40ac987

Please sign in to comment.