Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
if (options.connectivityType === NatConnectivityType.PUBLIC && !this._internetGatewayId) {
throw new Error('Cannot add a Public NAT Gateway without an Internet Gateway enabled on VPC');
}
return new NatGateway(this, 'NATGateway', {
return new NatGateway(this, `NATGateway-${options.subnet.node.id}`, {
vpc: this,
...options,
});
Expand Down
46 changes: 45 additions & 1 deletion packages/@aws-cdk/aws-ec2-alpha/test/vpc-add-method.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Template } from 'aws-cdk-lib/assertions';
import { Match, Template } from 'aws-cdk-lib/assertions';
import * as cdk from 'aws-cdk-lib';
import * as vpc from '../lib/vpc-v2';
import { IpCidr, SubnetV2 } from '../lib/subnet-v2';
Expand Down Expand Up @@ -541,4 +541,48 @@ describe('Vpc V2 with full control', () => {
PeerRegion: { Ref: 'AWS::Region' },
});
});

test('can add multiple NAT gateways to different subnets', () => {
// Add Internet Gateway first since public NAT gateways require it
myVpc.addInternetGateway();

// Add first NAT gateway
myVpc.addNatGateway({
subnet: mySubnet,
natGatewayName: 'FirstNatGateway',
connectivityType: route.NatConnectivityType.PUBLIC,
});

// Add second NAT gateway
myVpc.addNatGateway({
subnet: testSubnet1,
natGatewayName: 'SecondNatGateway',
connectivityType: route.NatConnectivityType.PUBLIC,
});

const template = Template.fromStack(stack);

// Verify that two NAT gateways are created
template.resourceCountIs('AWS::EC2::NatGateway', 2);

// Verify NAT Gateway 1 properties
template.hasResourceProperties('AWS::EC2::NatGateway', {
Tags: Match.arrayWith([
{
Key: 'Name',
Value: 'FirstNatGateway',
},
]),
});

// Verify NAT Gateway 2 properties
template.hasResourceProperties('AWS::EC2::NatGateway', {
Tags: Match.arrayWith([
{
Key: 'Name',
Value: 'SecondNatGateway',
},
]),
});
});
});
Loading