Skip to content

Commit

Permalink
change for determineNatGatewayCount
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Jan 7, 2024
1 parent c0943af commit 5d098ea
Show file tree
Hide file tree
Showing 2 changed files with 34 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 @@ -2403,7 +2403,7 @@ class ImportedSubnet extends Resource implements ISubnet, IPublicSubnet, IPrivat
function determineNatGatewayCount(requestedCount: number | undefined, subnetConfig: SubnetConfiguration[], azCount: number) {
const hasPrivateSubnets = subnetConfig.some(c => (c.subnetType === SubnetType.PRIVATE_WITH_EGRESS
|| c.subnetType === SubnetType.PRIVATE || c.subnetType === SubnetType.PRIVATE_WITH_NAT) && !c.reserved);
const hasPublicSubnets = subnetConfig.some(c => c.subnetType === SubnetType.PUBLIC);
const hasPublicSubnets = subnetConfig.some(c => c.subnetType === SubnetType.PUBLIC && !c.reserved);
const hasCustomEgress = subnetConfig.some(c => c.subnetType === SubnetType.PRIVATE_WITH_EGRESS);

const count = requestedCount !== undefined ? Math.min(requestedCount, azCount) : (hasPrivateSubnets ? azCount : 0);
Expand Down
33 changes: 33 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 @@ -399,6 +399,39 @@ describe('vpc', () => {

});

test('with no public subnets and natGateways > 0, should throw an error', () => {
const stack = getTestStack();
expect(() => new Vpc(stack, 'TheVPC', {
subnetConfiguration: [
{
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
name: 'egress',
},
],
natGateways: 1,
})).toThrow(/If you configure PRIVATE subnets in 'subnetConfiguration', you must also configure PUBLIC subnets to put the NAT gateways into \(got \[{"subnetType":"Private","name":"egress"}\]./);

});

test('with only reserved subnets as public subnets and natGateways > 0, should throw an error', () => {
const stack = getTestStack();
expect(() => new Vpc(stack, 'TheVPC', {
subnetConfiguration: [
{
subnetType: SubnetType.PUBLIC,
name: 'public',
reserved: true,
},
{
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
name: 'egress',
},
],
natGateways: 1,
})).toThrow(/If you configure PRIVATE subnets in 'subnetConfiguration', you must also configure PUBLIC subnets to put the NAT gateways into \(got \[{"subnetType":"Public","name":"public","reserved":true},{"subnetType":"Private","name":"egress"}\]./);

});

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 5d098ea

Please sign in to comment.