Skip to content

Commit 4b90bfc

Browse files
authored
feat(vpcv2): implementation of add gateway method (#31224)
### Issue # (if applicable) Tracking #[30762](#30762). ### Reason for this change implementing below methods for vpcV2. `routeTable.addroute(destination, target)`: Adds a new route to the existing route table of the subnet. `vpc.enableVpnGatewayV2()`: added a new function for the customer to add VPNGateway to their VPC. In the options, user can specify list of subnets for VPNRoutePropogation. This is similar to previous implementation, only difference is with VPNGateway L2, it is now creating VPNGatewayV2 which implements IRouteTarget and hence can be used a destination to be set up in route tables. `addInternetGateway` : adds internetGW to the VPC. **Default behaviour:** add default route with destination set to ‘0.0.0.0’ and ‘::0’(in case of subnet with ipv6). Also a check in place to verify SubnetType is set to public as IGW is meant to be added to public subnets. `addNatGateway`: NatGateways are subnet specific and are usually associated with PRIVATE_WITH_EGRESS or PUBLIC subnet. Also, one can’t attach NGW(Public) to subnet if VPC doesn’t have an IGW attached to it. This is validated in method implementation to prevent runtime deployment error. **No default behaviour** for the routes, it takes in the single subnet option and associates a NATGW with it. `vpc.addEgressOnlyInternetGateway()`: Egress Only internet GW are meant for outbound ipv6 traffic which can be custom or all ipv6(::/0). **Default behaviour:** Associates a EIGW to the vpc and takes optional input for subnets to define a default route in associated route Table, if a destination is not provided, then it is defined as all outbound ipv6 in subnet’s route table. **Additional changes:** -> Modify Readme -> Separate ipam related Tests ### Use Case Allows user to define gateways in their vpc with a simple method and an optional default route setup on provided subnets. Note: Breaking change since previously VPNGateway was released under route class, we’ve modified it to VPNGatewayV2. `vpc.enableVpnGateway` is marked as deprecated in vpcv2 base class. ### Description of how you validated changes Added unit tests and integration tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6d91ff3 commit 4b90bfc

File tree

62 files changed

+4568
-2148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4568
-2148
lines changed

packages/@aws-cdk/aws-ec2-alpha/README.md

Lines changed: 200 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ To create a VPC with both IPv4 and IPv6 support:
2727
```ts
2828

2929
const stack = new Stack();
30-
new vpc_v2.VpcV2(this, 'Vpc', {
31-
primaryAddressBlock: vpc_v2.IpAddresses.ipv4('10.0.0.0/24'),
30+
new VpcV2(this, 'Vpc', {
31+
primaryAddressBlock: IpAddresses.ipv4('10.0.0.0/24'),
3232
secondaryAddressBlocks: [
33-
vpc_v2.IpAddresses.amazonProvidedIpv6({cidrBlockName: 'AmazonProvidedIpv6'}),
33+
IpAddresses.amazonProvidedIpv6({cidrBlockName: 'AmazonProvidedIpv6'}),
3434
],
3535
});
3636
```
@@ -47,18 +47,18 @@ This new construct can be used to add subnets to a `VpcV2` instance:
4747
```ts
4848

4949
const stack = new Stack();
50-
const myVpc = new vpc_v2.VpcV2(this, 'Vpc', {
50+
const myVpc = new VpcV2(this, 'Vpc', {
5151
secondaryAddressBlocks: [
52-
vpc_v2.IpAddresses.amazonProvidedIpv6({ cidrBlockName: 'AmazonProvidedIp'}),
52+
IpAddresses.amazonProvidedIpv6({ cidrBlockName: 'AmazonProvidedIp'}),
5353
],
5454
});
5555

56-
new vpc_v2.SubnetV2(this, 'subnetA', {
56+
new SubnetV2(this, 'subnetA', {
5757
vpc: myVpc,
5858
availabilityZone: 'us-east-1a',
59-
ipv4CidrBlock: new vpc_v2.IpCidr('10.0.0.0/24'),
60-
ipv6CidrBlock: new vpc_v2.IpCidr('2a05:d02c:25:4000::/60'),
61-
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
59+
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
60+
ipv6CidrBlock: new IpCidr('2a05:d02c:25:4000::/60'),
61+
subnetType: SubnetType.PRIVATE_ISOLATED,
6262
})
6363
```
6464

@@ -77,28 +77,28 @@ const ipam = new Ipam(this, 'Ipam', {
7777
operatingRegion: ['us-west-1']
7878
});
7979
const ipamPublicPool = ipam.publicScope.addPool('PublicPoolA', {
80-
addressFamily: vpc_v2.AddressFamily.IP_V6,
80+
addressFamily: AddressFamily.IP_V6,
8181
awsService: AwsServiceName.EC2,
8282
locale: 'us-west-1',
83-
publicIpSource: vpc_v2.IpamPoolPublicIpSource.AMAZON,
83+
publicIpSource: IpamPoolPublicIpSource.AMAZON,
8484
});
8585
ipamPublicPool.provisionCidr('PublicPoolACidrA', { netmaskLength: 52 } );
8686

8787
const ipamPrivatePool = ipam.privateScope.addPool('PrivatePoolA', {
88-
addressFamily: vpc_v2.AddressFamily.IP_V4,
88+
addressFamily: AddressFamily.IP_V4,
8989
});
9090
ipamPrivatePool.provisionCidr('PrivatePoolACidrA', { netmaskLength: 8 } );
9191

92-
new vpc_v2.VpcV2(this, 'Vpc', {
93-
primaryAddressBlock: vpc_v2.IpAddresses.ipv4('10.0.0.0/24'),
92+
new VpcV2(this, 'Vpc', {
93+
primaryAddressBlock: IpAddresses.ipv4('10.0.0.0/24'),
9494
secondaryAddressBlocks: [
95-
vpc_v2.IpAddresses.amazonProvidedIpv6({ cidrBlockName: 'AmazonIpv6' }),
96-
vpc_v2.IpAddresses.ipv6Ipam({
95+
IpAddresses.amazonProvidedIpv6({ cidrBlockName: 'AmazonIpv6' }),
96+
IpAddresses.ipv6Ipam({
9797
ipamPool: ipamPublicPool,
9898
netmaskLength: 52,
9999
cidrBlockName: 'ipv6Ipam',
100100
}),
101-
vpc_v2.IpAddresses.ipv4Ipam({
101+
IpAddresses.ipv4Ipam({
102102
ipamPool: ipamPrivatePool,
103103
netmaskLength: 8,
104104
cidrBlockName: 'ipv4Ipam',
@@ -116,64 +116,88 @@ Since `VpcV2` does not create subnets automatically, users have full control ove
116116

117117
```ts
118118

119-
const myVpc = new vpc_v2.VpcV2(this, 'Vpc');
120-
const routeTable = new vpc_v2.RouteTable(this, 'RouteTable', {
119+
const myVpc = new VpcV2(this, 'Vpc');
120+
const routeTable = new RouteTable(this, 'RouteTable', {
121121
vpc: myVpc,
122122
});
123-
const subnet = new vpc_v2.SubnetV2(this, 'Subnet', {
123+
const subnet = new SubnetV2(this, 'Subnet', {
124124
vpc: myVpc,
125125
routeTable,
126126
availabilityZone: 'eu-west-2a',
127127
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
128-
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
128+
subnetType: SubnetType.PRIVATE_ISOLATED,
129129
});
130130
```
131131

132-
`Route`s can be created to link subnets to various different AWS services via gateways and endpoints. Each unique route target has its own dedicated construct that can be routed to a given subnet via the `Route` construct. An example using the `InternetGateway` construct can be seen below:
132+
`Routes` can be created to link subnets to various different AWS services via gateways and endpoints. Each unique route target has its own dedicated construct that can be routed to a given subnet via the `Route` construct. An example using the `InternetGateway` construct can be seen below:
133133

134134
```ts
135135
const stack = new Stack();
136-
const myVpc = new vpc_v2.VpcV2(this, 'Vpc');
137-
const routeTable = new vpc_v2.RouteTable(this, 'RouteTable', {
136+
const myVpc = new VpcV2(this, 'Vpc');
137+
const routeTable = new RouteTable(this, 'RouteTable', {
138138
vpc: myVpc,
139139
});
140-
const subnet = new vpc_v2.SubnetV2(this, 'Subnet', {
140+
const subnet = new SubnetV2(this, 'Subnet', {
141141
vpc: myVpc,
142142
availabilityZone: 'eu-west-2a',
143143
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
144-
subnetType: ec2.SubnetType.PRIVATE_ISOLATED });
144+
subnetType: SubnetType.PRIVATE_ISOLATED });
145145

146-
const igw = new vpc_v2.InternetGateway(this, 'IGW', {
146+
const igw = new InternetGateway(this, 'IGW', {
147147
vpc: myVpc,
148148
});
149-
new vpc_v2.Route(this, 'IgwRoute', {
149+
new Route(this, 'IgwRoute', {
150150
routeTable,
151151
destination: '0.0.0.0/0',
152152
target: { gateway: igw },
153153
});
154154
```
155155

156+
Alternatively, `Routes` can also be created via method `addRoute` in the `RouteTable` class. An example using the `EgressOnlyInternetGateway` construct can be seen below:
157+
Note: `EgressOnlyInternetGateway` can only be used to set up outbound IPv6 routing.
158+
159+
```ts
160+
161+
const stack = new Stack();
162+
const myVpc = new VpcV2(this, 'Vpc',{
163+
primaryAddressBlock: IpAddresses.ipv4('10.1.0.0/16'),
164+
secondaryAddressBlocks: [IpAddresses.amazonProvidedIpv6({
165+
cidrBlockName: 'AmazonProvided',
166+
})]
167+
});
168+
169+
const eigw = new EgressOnlyInternetGateway(this, 'EIGW', {
170+
vpc: myVpc,
171+
});
172+
173+
const routeTable = new RouteTable(this, 'RouteTable', {
174+
vpc: myVpc,
175+
});
176+
177+
routeTable.addRoute('EIGW', '::/0', { gateway: eigw });
178+
```
179+
156180
Other route targets may require a deeper set of parameters to set up properly. For instance, the example below illustrates how to set up a `NatGateway`:
157181

158182
```ts
159183

160-
const myVpc = new vpc_v2.VpcV2(this, 'Vpc');
161-
const routeTable = new vpc_v2.RouteTable(this, 'RouteTable', {
184+
const myVpc = new VpcV2(this, 'Vpc');
185+
const routeTable = new RouteTable(this, 'RouteTable', {
162186
vpc: myVpc,
163187
});
164-
const subnet = new vpc_v2.SubnetV2(this, 'Subnet', {
188+
const subnet = new SubnetV2(this, 'Subnet', {
165189
vpc: myVpc,
166190
availabilityZone: 'eu-west-2a',
167191
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
168-
subnetType: ec2.SubnetType.PRIVATE_ISOLATED });
192+
subnetType: SubnetType.PRIVATE_ISOLATED });
169193

170-
const natgw = new vpc_v2.NatGateway(this, 'NatGW', {
194+
const natgw = new NatGateway(this, 'NatGW', {
171195
subnet: subnet,
172196
vpc: myVpc,
173197
connectivityType: NatConnectivityType.PRIVATE,
174198
privateIpAddress: '10.0.0.42',
175199
});
176-
new vpc_v2.Route(this, 'NatGwRoute', {
200+
new Route(this, 'NatGwRoute', {
177201
routeTable,
178202
destination: '0.0.0.0/0',
179203
target: { gateway: natgw },
@@ -184,24 +208,161 @@ It is also possible to set up endpoints connecting other AWS services. For insta
184208

185209
```ts
186210

187-
const myVpc = new vpc_v2.VpcV2(this, 'Vpc');
188-
const routeTable = new vpc_v2.RouteTable(this, 'RouteTable', {
211+
const stack = new Stack();
212+
const myVpc = new VpcV2(this, 'Vpc');
213+
const routeTable = new RouteTable(this, 'RouteTable', {
189214
vpc: myVpc,
190215
});
191-
const subnet = new vpc_v2.SubnetV2(this, 'Subnet', {
216+
const subnet = new SubnetV2(this, 'Subnet', {
192217
vpc: myVpc,
193218
availabilityZone: 'eu-west-2a',
194219
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
195-
subnetType: ec2.SubnetType.PRIVATE });
220+
subnetType: SubnetType.PRIVATE });
196221

197222
const dynamoEndpoint = new ec2.GatewayVpcEndpoint(this, 'DynamoEndpoint', {
198223
service: ec2.GatewayVpcEndpointAwsService.DYNAMODB,
199224
vpc: myVpc,
200225
subnets: [subnet],
201226
});
202-
new vpc_v2.Route(this, 'DynamoDBRoute', {
227+
new Route(this, 'DynamoDBRoute', {
203228
routeTable,
204229
destination: '0.0.0.0/0',
205230
target: { endpoint: dynamoEndpoint },
206231
});
207232
```
233+
234+
## Adding Egress-Only Internet Gateway to VPC
235+
236+
An egress-only internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows outbound communication over IPv6 from instances in your VPC to the internet, and prevents the internet from initiating an IPv6 connection with your instances.
237+
238+
For more information see [Enable outbound IPv6 traffic using an egress-only internet gateway](https://docs.aws.amazon.com/vpc/latest/userguide/egress-only-internet-gateway.html).
239+
240+
VpcV2 supports adding an egress only internet gateway to VPC using the `addEgressOnlyInternetGateway` method.
241+
242+
By default, this method sets up a route to all outbound IPv6 address ranges, unless a specific destination is provided by the user. It can only be configured for IPv6-enabled VPCs.
243+
The `Subnets` parameter accepts a `SubnetFilter`, which can be based on a `SubnetType` in VpcV2. A new route will be added to the route tables of all subnets that match this filter.
244+
245+
```ts
246+
247+
const stack = new Stack();
248+
const myVpc = new VpcV2(this, 'Vpc',{
249+
primaryAddressBlock: IpAddresses.ipv4('10.1.0.0/16'),
250+
secondaryAddressBlocks: [IpAddresses.amazonProvidedIpv6({
251+
cidrBlockName: 'AmazonProvided',
252+
})]
253+
});
254+
const routeTable = new RouteTable(this, 'RouteTable', {
255+
vpc: myVpc,
256+
});
257+
const subnet = new SubnetV2(this, 'Subnet', {
258+
vpc: myVpc,
259+
availabilityZone: 'eu-west-2a',
260+
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
261+
ipv6CidrBlock: new IpCidr('2001:db8:1::/64'),
262+
subnetType: SubnetType.PRIVATE });
263+
264+
myVpc.addEgressOnlyInternetGateway({
265+
subnets: [{subnetType: SubnetType.PRIVATE}],
266+
destination: '::/60',
267+
})
268+
```
269+
270+
## Adding NATGateway to the VPC
271+
272+
A NAT gateway is a Network Address Translation (NAT) service.You can use a NAT gateway so that instances in a private subnet can connect to services outside your VPC but external services cannot initiate a connection with those instances.
273+
274+
For more information, see [NAT gateway basics](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html).
275+
276+
When you create a NAT gateway, you specify one of the following connectivity types:
277+
278+
**Public – (Default)**: Instances in private subnets can connect to the internet through a public NAT gateway, but cannot receive unsolicited inbound connections from the internet
279+
280+
**Private**: Instances in private subnets can connect to other VPCs or your on-premises network through a private NAT gateway.
281+
282+
To define the NAT gateway connectivity type as `ConnectivityType.Public`, you need to ensure that there is an IGW(Internet Gateway) attached to the subnet's VPC.
283+
Since a NATGW is associated with a particular subnet, providing `subnet` field in the input props is mandatory.
284+
285+
Additionally, you can set up a route in any route table with the target set to the NAT Gateway. The function `addNatGateway` returns a `NATGateway` object that you can reference later.
286+
287+
The code example below provides the definition for adding a NAT gateway to your subnet:
288+
289+
```ts
290+
291+
const stack = new Stack();
292+
const myVpc = new VpcV2(this, 'Vpc');
293+
const routeTable = new RouteTable(this, 'RouteTable', {
294+
vpc: myVpc,
295+
});
296+
const subnet = new SubnetV2(this, 'Subnet', {
297+
vpc: myVpc,
298+
availabilityZone: 'eu-west-2a',
299+
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
300+
subnetType: SubnetType.PUBLIC });
301+
302+
myVpc.addInternetGateway();
303+
myVpc.addNatGateway({
304+
subnet: subnet,
305+
connectivityType: NatConnectivityType.PUBLIC,
306+
});
307+
```
308+
309+
## Enable VPNGateway for the VPC
310+
311+
A virtual private gateway is the endpoint on the VPC side of your VPN connection.
312+
313+
For more information, see [What is AWS Site-to-Site VPN?](https://docs.aws.amazon.com/vpn/latest/s2svpn/VPC_VPN.html).
314+
315+
VPN route propagation is a feature in Amazon Web Services (AWS) that automatically updates route tables in your Virtual Private Cloud (VPC) with routes learned from a VPN connection.
316+
317+
To enable VPN route propogation, use the `vpnRoutePropagation` property to specify the subnets as an input to the function. VPN route propagation will then be enabled for each subnet with the corresponding route table IDs.
318+
319+
Additionally, you can set up a route in any route table with the target set to the VPN Gateway. The function `enableVpnGatewayV2` returns a `VPNGatewayV2` object that you can reference later.
320+
321+
The code example below provides the definition for setting up a VPN gateway with `vpnRoutePropogation` enabled:
322+
323+
```ts
324+
325+
const stack = new Stack();
326+
const myVpc = new VpcV2(this, 'Vpc');
327+
const vpnGateway = myVpc.enableVpnGatewayV2({
328+
vpnRoutePropagation: [{ subnetType: SubnetType.PUBLIC }],
329+
type: VpnConnectionType.IPSEC_1,
330+
});
331+
332+
const routeTable = new RouteTable(stack, 'routeTable', {
333+
vpc: myVpc
334+
} );
335+
336+
new Route(stack, 'route', {
337+
destination: '172.31.0.0/24',
338+
target: { gateway: vpnGateway },
339+
routeTable: routeTable,
340+
});
341+
```
342+
343+
## Adding InternetGateway to the VPC
344+
345+
An internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the internet. It supports both IPv4 and IPv6 traffic.
346+
347+
For more information, see [Enable VPC internet access using internet gateways](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-igw-internet-access.html).
348+
349+
You can add an internet gateway to a VPC using `addInternetGateway` method. By default, this method creates a route in all Public Subnets with outbound destination set to `0.0.0.0` for IPv4 and `::0` for IPv6 enabled VPC.
350+
Instead of using the default settings, you can configure a custom destinatation range by providing an optional input `destination` to the method.
351+
352+
The code example below shows how to add an internet gateway with a custom outbound destination IP range:
353+
354+
```ts
355+
356+
const stack = new Stack();
357+
const myVpc = new VpcV2(this, 'Vpc');
358+
359+
const subnet = new SubnetV2(this, 'Subnet', {
360+
vpc: myVpc,
361+
availabilityZone: 'eu-west-2a',
362+
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
363+
subnetType: SubnetType.PUBLIC });
364+
365+
myVpc.addInternetGateway({
366+
ipv4Destination: '192.168.0.0/16',
367+
});
368+
```

packages/@aws-cdk/aws-ec2-alpha/awslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"exclude": [
33
"from-method:@aws-cdk/aws-ec2-alpha.VpcV2",
44
"attribute-tag:@aws-cdk/aws-ec2-alpha.RouteTable.routeTableId",
5-
"from-method:@aws-cdk/aws-ec2-alpha.SubnetV2"
5+
"from-method:@aws-cdk/aws-ec2-alpha.SubnetV2",
6+
"from-method:@aws-cdk/aws-ec2-alpha.Route"
67
]
78
}

0 commit comments

Comments
 (0)