Skip to content

Commit 578debf

Browse files
authored
chore(ec2-alpha): adding assertion for integration tests (#33221)
### Issue # (if applicable) Closes N/A ### Reason for this change Added assertion for the integration tests that are related to security changes in the construct, needed for appsec approval. ### Description of changes - Added integration with assertion for custom EIGW and IGW route. - Added integration with assertion for VPC peering - Fixing nits in README. ### Describe any new or updated permissions being added No change in permissions ### Description of how you validated changes Deployed assertion changes in personal account. yarn build yarn test ### 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 71c492a commit 578debf

22 files changed

+64335
-27
lines changed

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

+10-26
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
on the VPC being created. `VpcV2` implements the existing [`IVpc`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IVpc.html), therefore,
2323
`VpcV2` is compatible with other constructs that accepts `IVpc` (e.g. [`ApplicationLoadBalancer`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.ApplicationLoadBalancer.html#construct-props)).
2424

25-
To create a VPC with both IPv4 and IPv6 support:
25+
`VpcV2` supports the addition of both primary and secondary addresses. The primary address must be an IPv4 address, which can be specified as a CIDR string or assigned from an IPAM pool. Secondary addresses can be either IPv4 or IPv6.
26+
By default, `VpcV2` assigns `10.0.0.0/16` as the primary CIDR if no other CIDR is specified.
2627

27-
```ts
28+
Below is an example of creating a VPC with both IPv4 and IPv6 support:
2829

30+
```ts
2931
const stack = new Stack();
3032
new VpcV2(this, 'Vpc', {
3133
primaryAddressBlock: IpAddresses.ipv4('10.0.0.0/24'),
@@ -44,7 +46,6 @@ This new construct can be used to add subnets to a `VpcV2` instance:
4446
Note: When defining a subnet with `SubnetV2`, CDK automatically creates a new route table, unless a route table is explicitly provided as an input to the construct.
4547

4648
```ts
47-
4849
const stack = new Stack();
4950
const myVpc = new VpcV2(this, 'Vpc', {
5051
secondaryAddressBlocks: [
@@ -61,11 +62,12 @@ new SubnetV2(this, 'subnetA', {
6162
})
6263
```
6364

65+
Since `VpcV2` does not create subnets automatically, users have full control over IP addresses allocation across subnets.
66+
6467
## IP Addresses Management
6568

66-
By default `VpcV2` uses `10.0.0.0/16` as the primary CIDR if none is defined.
67-
Additional CIDRs can be adding to the VPC via the `secondaryAddressBlocks` prop.
68-
The following example illustrates the different options of defining the address blocks:
69+
Additional CIDRs can be added to the VPC via the `secondaryAddressBlocks` property.
70+
The following example illustrates the options of defining these secondary address blocks using `IPAM`:
6971

7072
Note: There’s currently an issue with IPAM pool deletion that may affect the `cdk --destroy` command. This is because IPAM takes time to detect when the IP address pool has been deallocated after the VPC is deleted. The current workaround is to wait until the IP address is fully deallocated from the pool before retrying the deletion. Below command can be used to check allocations for a pool using CLI
7173

@@ -76,7 +78,6 @@ aws ec2 get-ipam-pool-allocations --ipam-pool-id <ipam-pool-id>
7678
Ref: https://docs.aws.amazon.com/cli/latest/reference/ec2/get-ipam-pool-allocations.html
7779

7880
```ts
79-
8081
const stack = new Stack();
8182
const ipam = new Ipam(this, 'Ipam', {
8283
operatingRegions: ['us-west-1']
@@ -112,8 +113,6 @@ new VpcV2(this, 'Vpc', {
112113
});
113114
```
114115

115-
Since `VpcV2` does not create subnets automatically, users have full control over IP addresses allocation across subnets.
116-
117116
### Bring your own IPv6 addresses (BYOIP)
118117

119118
If you have your own IP address that you would like to use with EC2, you can set up an IPv6 pool via the AWS CLI, and use that pool ID in your application.
@@ -149,10 +148,10 @@ const myVpc = new VpcV2(this, 'Vpc', {
149148

150149
## Routing
151150

152-
`RouteTable` is a new construct that allows for route tables to be customized in a variety of ways. For instance, the following example shows how a custom route table can be created and appended to a subnet:
151+
`RouteTable` is a new construct that allows for route tables to be customized in a variety of ways. Using this construct, a customized route table can be added to the subnets defined using `SubnetV2`.
152+
For instance, the following example shows how a custom route table can be created and appended to a `SubnetV2`:
153153

154154
```ts
155-
156155
const myVpc = new VpcV2(this, 'Vpc');
157156
const routeTable = new RouteTable(this, 'RouteTable', {
158157
vpc: myVpc,
@@ -194,7 +193,6 @@ Alternatively, `Routes` can also be created via method `addRoute` in the `RouteT
194193
Note: `EgressOnlyInternetGateway` can only be used to set up outbound IPv6 routing.
195194

196195
```ts
197-
198196
const stack = new Stack();
199197
const myVpc = new VpcV2(this, 'Vpc',{
200198
primaryAddressBlock: IpAddresses.ipv4('10.1.0.0/16'),
@@ -217,7 +215,6 @@ routeTable.addRoute('EIGW', '::/0', { gateway: eigw });
217215
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`:
218216

219217
```ts
220-
221218
const myVpc = new VpcV2(this, 'Vpc');
222219
const routeTable = new RouteTable(this, 'RouteTable', {
223220
vpc: myVpc,
@@ -244,7 +241,6 @@ new Route(this, 'NatGwRoute', {
244241
It is also possible to set up endpoints connecting other AWS services. For instance, the example below illustrates the linking of a Dynamo DB endpoint via the existing `ec2.GatewayVpcEndpoint` construct as a route target:
245242

246243
```ts
247-
248244
const stack = new Stack();
249245
const myVpc = new VpcV2(this, 'Vpc');
250246
const routeTable = new RouteTable(this, 'RouteTable', {
@@ -266,7 +262,6 @@ new Route(this, 'DynamoDBRoute', {
266262
destination: '0.0.0.0/0',
267263
target: { endpoint: dynamoEndpoint },
268264
});
269-
270265
```
271266

272267
## VPC Peering Connection
@@ -431,7 +426,6 @@ By default, this method sets up a route to all outbound IPv6 address ranges, unl
431426
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.
432427

433428
```ts
434-
435429
const stack = new Stack();
436430
const myVpc = new VpcV2(this, 'Vpc',{
437431
primaryAddressBlock: IpAddresses.ipv4('10.1.0.0/16'),
@@ -475,7 +469,6 @@ Additionally, you can set up a route in any route table with the target set to t
475469
The code example below provides the definition for adding a NAT gateway to your subnet:
476470

477471
```ts
478-
479472
const stack = new Stack();
480473
const myVpc = new VpcV2(this, 'Vpc');
481474
const routeTable = new RouteTable(this, 'RouteTable', {
@@ -509,7 +502,6 @@ Additionally, you can set up a route in any route table with the target set to t
509502
The code example below provides the definition for setting up a VPN gateway with `vpnRoutePropagation` enabled:
510503

511504
```ts
512-
513505
const stack = new Stack();
514506
const myVpc = new VpcV2(this, 'Vpc');
515507
const vpnGateway = myVpc.enableVpnGatewayV2({
@@ -541,7 +533,6 @@ In addition to the custom IP range, you can also choose to filter subnets where
541533
The code example below shows how to add an internet gateway with a custom outbound destination IP range:
542534

543535
```ts
544-
545536
const stack = new Stack();
546537
const myVpc = new VpcV2(this, 'Vpc');
547538

@@ -597,22 +588,19 @@ If you wish to add a new subnet to imported VPC, new subnet's IP range(IPv4) wil
597588
Here's an example of importing a VPC with only the required parameters
598589

599590
``` ts
600-
601591
const stack = new Stack();
602592

603593
const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', {
604594
vpcId: 'mockVpcID',
605595
vpcCidrBlock: '10.0.0.0/16',
606596
});
607-
608597
```
609598

610599
In case of cross account or cross region VPC, its recommended to provide region and ownerAccountId so that these values for the VPC can be used to populate correct arn value for the VPC. If a VPC region and account ID is not provided, then region and account configured in the stack will be used. Furthermore, these fields will be referenced later while setting up VPC peering connection, so its necessary to set these fields to a correct value.
611600

612601
Below is an example of importing a cross region and cross account VPC, VPC arn for this case would be 'arn:aws:ec2:us-west-2:123456789012:vpc/mockVpcID'
613602

614603
``` ts
615-
616604
const stack = new Stack();
617605

618606
//Importing a cross account or cross region VPC
@@ -622,7 +610,6 @@ const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', {
622610
ownerAccountId: '123456789012',
623611
region: 'us-west-2',
624612
});
625-
626613
```
627614

628615
Here's an example of how to import a VPC with multiple CIDR blocks, IPv6 support, and different subnet types:
@@ -637,7 +624,6 @@ In this example, we're importing a VPC with:
637624
- A public subnet in us-west-2b
638625

639626
```ts
640-
641627
const stack = new Stack();
642628

643629
const importedVpc = VpcV2.fromVpcV2Attributes(this, 'ImportedVPC', {
@@ -704,7 +690,6 @@ You can also import individual subnets using the `SubnetV2.fromSubnetV2Attribute
704690
Here's an example of how to import a subnet:
705691

706692
```ts
707-
708693
SubnetV2.fromSubnetV2Attributes(this, 'ImportedSubnet', {
709694
subnetId: 'subnet-0123456789abcdef0',
710695
availabilityZone: 'us-west-2a',
@@ -723,7 +708,6 @@ By default, when a resource name is given to the construct, it automatically add
723708
For example, if the `vpcName` is set to `TestVpc`, the following code will add a tag to the VPC with `key: Name` and `value: TestVpc`.
724709

725710
```ts
726-
727711
const vpc = new VpcV2(this, 'VPC-integ-test-tag', {
728712
primaryAddressBlock: IpAddresses.ipv4('10.1.0.0/16'),
729713
enableDnsHostnames: true,

packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,13 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
299299
* Mutable private field for the internetGatewayId
300300
* @internal
301301
*/
302-
protected _internetGatewayId = '';
302+
protected _internetGatewayId?: string;
303+
304+
/**
305+
* Mutable private field for the EgressOnlyInternetGatewayId
306+
* @internal
307+
*/
308+
protected _egressOnlyInternetGatewayId?: string;
303309

304310
/**
305311
* Return information on the subnets appropriate for the given selection strategy
@@ -430,6 +436,7 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
430436
vpc: this,
431437
egressOnlyInternetGatewayName: options?.egressOnlyInternetGatewayName,
432438
});
439+
this._egressOnlyInternetGatewayId = egw.routerTargetId;
433440

434441
let useIpv6;
435442
if (this.secondaryCidrBlock) {
@@ -607,6 +614,13 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
607614
return this._internetGatewayId;
608615
}
609616

617+
/**
618+
* Returns the id of the Egress Only Internet Gateway (if enabled)
619+
*/
620+
public get egressOnlyInternetGatewayId(): string | undefined {
621+
return this._egressOnlyInternetGatewayId;
622+
}
623+
610624
/**
611625
* Return the subnets appropriate for the placement strategy
612626
*/

packages/@aws-cdk/aws-ec2-alpha/test/integ.vpc-add-gateways.js.snapshot/VpcSameAccountIntegDefaultTestDeployAssertDA1BF34D.assets.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)