@@ -21,10 +21,9 @@ on the VPC being created. `VpcV2` implements the existing [`IVpc`](https://docs.
21
21
To create a VPC with both IPv4 and IPv6 support:
22
22
23
23
``` ts
24
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
25
24
26
- const stack = new cdk . Stack (app , ' aws-cdk-vpcv2 ' );
27
- new vpc_v2 .VpcV2 (stack , ' Vpc' , {
25
+ const stack = new Stack ();
26
+ new vpc_v2 .VpcV2 (this , ' Vpc' , {
28
27
primaryAddressBlock: vpc_v2 .IpAddresses .ipv4 (' 10.0.0.0/24' ),
29
28
secondaryAddressBlocks: [
30
29
vpc_v2 .IpAddresses .amazonProvidedIpv6 ({cidrBlockName: ' AmazonProvidedIpv6' }),
@@ -42,22 +41,19 @@ Importing existing VPC in an account into CDK as a `VpcV2` is not yet supported.
42
41
This new construct can be used to add subnets to a ` VpcV2 ` instance:
43
42
44
43
``` ts
45
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
46
- import * as ec2 from ' aws-cdk-lib/aws-ec2' ;
47
44
48
- const stack = new cdk . Stack (app , ' aws-cdk-vpcv2 ' );
49
- const vpc = new vpc_v2 .VpcV2 (stack , ' Vpc' , {
45
+ const stack = new Stack ();
46
+ const myVpc = new vpc_v2 .VpcV2 (this , ' Vpc' , {
50
47
secondaryAddressBlocks: [
51
48
vpc_v2 .IpAddresses .amazonProvidedIpv6 ({ cidrBlockName: ' AmazonProvidedIp' }),
52
49
],
53
50
});
54
- const vpcFirstIpV6Cidr = Fn .select (0 , vpc .ipv6CidrBlocks );
55
- const subCidrs = Fn .cidr (vpcFirstIpV6Cidr , 3 , 32 );
56
- new vpc_v2 .SubnetV2 (stack , ' subnetA' , {
57
- vpc ,
51
+
52
+ new vpc_v2 .SubnetV2 (this , ' subnetA' , {
53
+ vpc: myVpc ,
58
54
availabilityZone: ' us-east-1a' ,
59
- cidrBlock : new vpc_v2 .IpCidr (' 10.0.0.0/24' ),
60
- ipv6CidrBlock: new vpc_v2 .IpCidr (Fn . select ( 0 , subCidrs ) ),
55
+ ipv4CidrBlock : new vpc_v2 .IpCidr (' 10.0.0.0/24' ),
56
+ ipv6CidrBlock: new vpc_v2 .IpCidr (' 2a05:d02c:25:4000::/60 ' ),
61
57
subnetType: ec2 .SubnetType .PRIVATE_ISOLATED ,
62
58
})
63
59
```
@@ -71,15 +67,14 @@ Additional CIDRs can be adding to the VPC via the `secondaryAddressBlocks` prop.
71
67
The following example illustrates the different options of defining the address blocks:
72
68
73
69
``` ts
74
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
75
70
76
- const stack = new cdk . Stack (app , ' aws-cdk-vpcv2 ' );
77
- const ipam = new Ipam (stack , ' Ipam' , {
71
+ const stack = new Stack ();
72
+ const ipam = new Ipam (this , ' Ipam' , {
78
73
operatingRegion: [' us-west-1' ]
79
74
});
80
75
const ipamPublicPool = ipam .publicScope .addPool (' PublicPoolA' , {
81
76
addressFamily: vpc_v2 .AddressFamily .IP_V6 ,
82
- awsService: ' ec2 ' ,
77
+ awsService: AwsServiceName . EC2 ,
83
78
locale: ' us-west-1' ,
84
79
publicIpSource: vpc_v2 .IpamPoolPublicIpSource .AMAZON ,
85
80
});
@@ -90,18 +85,18 @@ const ipamPrivatePool = ipam.privateScope.addPool('PrivatePoolA', {
90
85
});
91
86
ipamPrivatePool .provisionCidr (' PrivatePoolACidrA' , { netmaskLength: 8 } );
92
87
93
- new vpc_v2 .VpcV2 (stack , ' Vpc' , {
88
+ new vpc_v2 .VpcV2 (this , ' Vpc' , {
94
89
primaryAddressBlock: vpc_v2 .IpAddresses .ipv4 (' 10.0.0.0/24' ),
95
90
secondaryAddressBlocks: [
96
91
vpc_v2 .IpAddresses .amazonProvidedIpv6 ({ cidrBlockName: ' AmazonIpv6' }),
97
92
vpc_v2 .IpAddresses .ipv6Ipam ({
98
- ipv6IpamPool : ipamPublicPool ,
99
- ipv6NetmaskLength : 52 ,
93
+ ipamPool : ipamPublicPool ,
94
+ netmaskLength : 52 ,
100
95
cidrBlockName: ' ipv6Ipam' ,
101
96
}),
102
97
vpc_v2 .IpAddresses .ipv4Ipam ({
103
- ipv6IpamPool : ipamPrivatePool ,
104
- ipv6NetmaskLength : 8 ,
98
+ ipamPool : ipamPrivatePool ,
99
+ netmaskLength : 8 ,
105
100
cidrBlockName: ' ipv4Ipam' ,
106
101
}),
107
102
],
@@ -116,84 +111,93 @@ Since `VpcV2` does not create subnets automatically, users have full control ove
116
111
` 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:
117
112
118
113
``` ts
119
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
120
114
121
- const myVpc = new vpc_v2 .VpcV2 (stack , ' Vpc' , { ... } );
122
- const routeTable = new vpc_v2 .RouteTable (stack , ' RouteTable' , {
115
+ const myVpc = new vpc_v2 .VpcV2 (this , ' Vpc' );
116
+ const routeTable = new vpc_v2 .RouteTable (this , ' RouteTable' , {
123
117
vpc: myVpc ,
124
118
});
125
- const subnet = new vpc_v2 .SubnetV2 (stack , ' Subnet' , {
126
- vpc ,
119
+ const subnet = new vpc_v2 .SubnetV2 (this , ' Subnet' , {
120
+ vpc: myVpc ,
127
121
routeTable ,
128
- ... ,
122
+ availabilityZone: ' eu-west-2a' ,
123
+ ipv4CidrBlock: new IpCidr (' 10.0.0.0/24' ),
124
+ subnetType: ec2 .SubnetType .PRIVATE_ISOLATED ,
129
125
});
130
126
```
131
127
132
128
` 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:
133
129
134
130
``` ts
135
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
136
-
137
- const myVpc = new vpc_v2 .VpcV2 (stack , ' Vpc' , {... });
138
- const routeTable = new vpc_v2 .RouteTable (stack , ' RouteTable' , {
139
- vpc: vpc .myVpc ,
131
+ const stack = new Stack ();
132
+ const myVpc = new vpc_v2 .VpcV2 (this , ' Vpc' );
133
+ const routeTable = new vpc_v2 .RouteTable (this , ' RouteTable' , {
134
+ vpc: myVpc ,
140
135
});
141
- const subnet = new vpc_v2 .SubnetV2 (stack , ' Subnet' , {... });
136
+ const subnet = new vpc_v2 .SubnetV2 (this , ' Subnet' , {
137
+ vpc: myVpc ,
138
+ availabilityZone: ' eu-west-2a' ,
139
+ ipv4CidrBlock: new IpCidr (' 10.0.0.0/24' ),
140
+ subnetType: ec2 .SubnetType .PRIVATE_ISOLATED });
142
141
143
- const igw = new vpc_v2 .InternetGateway (stack , ' IGW' , {
144
- vpcId: vpc . myVpc ,
142
+ const igw = new vpc_v2 .InternetGateway (this , ' IGW' , {
143
+ vpc: myVpc ,
145
144
});
146
- new vpc_v2 .Route (stack , ' IgwRoute' , {
145
+ new vpc_v2 .Route (this , ' IgwRoute' , {
147
146
routeTable ,
148
- destination: vpc_v2 . IpAddresses . ipv4 ( ' 0.0.0.0/0' ) ,
149
- target: igw ,
147
+ destination: ' 0.0.0.0/0' ,
148
+ target: { gateway: igw } ,
150
149
});
151
150
```
152
151
153
152
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 ` :
154
153
155
154
``` ts
156
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
157
155
158
- const myVpc = new vpc_v2 .VpcV2 (stack , ' Vpc' , { ... } );
159
- const routeTable = new vpc_v2 .RouteTable (stack , ' RouteTable' , {
160
- vpcId: vpc . myVpc ,
156
+ const myVpc = new vpc_v2 .VpcV2 (this , ' Vpc' );
157
+ const routeTable = new vpc_v2 .RouteTable (this , ' RouteTable' , {
158
+ vpc: myVpc ,
161
159
});
162
- const subnet = new vpc_v2 .SubnetV2 (stack , ' Subnet' , {... });
160
+ const subnet = new vpc_v2 .SubnetV2 (this , ' Subnet' , {
161
+ vpc: myVpc ,
162
+ availabilityZone: ' eu-west-2a' ,
163
+ ipv4CidrBlock: new IpCidr (' 10.0.0.0/24' ),
164
+ subnetType: ec2 .SubnetType .PRIVATE_ISOLATED });
163
165
164
- const natgw = new vpc_v2 .NatGateway (stack , ' NatGW' , {
166
+ const natgw = new vpc_v2 .NatGateway (this , ' NatGW' , {
165
167
subnet: subnet ,
166
- vpcId: vpc . myVpc ,
167
- connectivityType: ' private ' ,
168
+ vpc: myVpc ,
169
+ connectivityType: NatConnectivityType . PRIVATE ,
168
170
privateIpAddress: ' 10.0.0.42' ,
169
171
});
170
- new vpc_v2 .Route (stack , ' NatGwRoute' , {
172
+ new vpc_v2 .Route (this , ' NatGwRoute' , {
171
173
routeTable ,
172
- destination: vpc_v2 . IpAddresses . ipv4 ( ' 0.0.0.0/0' ) ,
173
- target: natgw ,
174
+ destination: ' 0.0.0.0/0' ,
175
+ target: { gateway: natgw } ,
174
176
});
175
177
```
176
178
177
179
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:
178
180
179
181
``` ts
180
- import * as vpc_v2 from ' @aws-cdk/aws-ec2-alpha' ;
181
- import * as ec2 from ' aws-cdk-lib/aws-ec2' ;
182
182
183
- const myVpc = new vpc_v2 .VpcV2 (stack , ' Vpc' , { ... } );
184
- const routeTable = new vpc_v2 .RouteTable (stack , ' RouteTable' , {
185
- vpcId: vpc . myVpc ,
183
+ const myVpc = new vpc_v2 .VpcV2 (this , ' Vpc' );
184
+ const routeTable = new vpc_v2 .RouteTable (this , ' RouteTable' , {
185
+ vpc: myVpc ,
186
186
});
187
- const subnet = new vpc_v2 .SubnetV2 (stack , ' Subnet' , {... });
187
+ const subnet = new vpc_v2 .SubnetV2 (this , ' Subnet' , {
188
+ vpc: myVpc ,
189
+ availabilityZone: ' eu-west-2a' ,
190
+ ipv4CidrBlock: new IpCidr (' 10.0.0.0/24' ),
191
+ subnetType: ec2 .SubnetType .PRIVATE });
188
192
189
- const dynamoEndpoint = new GatewayVpcEndpoint (stack , ' DynamoEndpoint' , {
193
+ const dynamoEndpoint = new ec2 . GatewayVpcEndpoint (this , ' DynamoEndpoint' , {
190
194
service: ec2 .GatewayVpcEndpointAwsService .DYNAMODB ,
191
- vpc: vpc ,
195
+ vpc: myVpc ,
192
196
subnets: [subnet ],
193
197
});
194
- new vpc_v2 .Route (stack , ' DynamoDBRoute' , {
198
+ new vpc_v2 .Route (this , ' DynamoDBRoute' , {
195
199
routeTable ,
196
- destination: vpc_v2 . IpAddresses . ipv4 ( ' 0.0.0.0/0' ) ,
197
- target: dynamoEndpoint ,
200
+ destination: ' 0.0.0.0/0' ,
201
+ target: { endpoint: dynamoEndpoint } ,
198
202
});
199
203
```
0 commit comments