1212The ` @aws-cdk/aws-ec2 ` package contains primitives for setting up networking and
1313instances.
1414
15+ ``` ts nofixture
16+ import ec2 = require (' @aws-cdk/aws-ec2' );
17+ ```
18+
1519## VPC
1620
1721Most projects need a Virtual Private Cloud to provide security by means of
1822network partitioning. This is achieved by creating an instance of
1923` Vpc ` :
2024
2125``` ts
22- import ec2 = require (' @aws-cdk/aws-ec2' );
23-
2426const vpc = new ec2 .Vpc (this , ' VPC' );
2527```
2628
@@ -186,7 +188,6 @@ by setting the `reserved` subnetConfiguration property to true, as shown
186188below:
187189
188190``` ts
189- import ec2 = require (' @aws-cdk/aws-ec2' );
190191const vpc = new ec2 .Vpc (this , ' TheVPC' , {
191192 natGateways: 1 ,
192193 subnetConfiguration: [
@@ -263,7 +264,7 @@ which you can add egress traffic rules.
263264
264265You can manipulate Security Groups directly:
265266
266- ``` ts
267+ ``` ts fixture=with-vpc
267268const mySecurityGroup = new ec2 .SecurityGroup (this , ' SecurityGroup' , {
268269 vpc ,
269270 description: ' Allow ssh access to ec2 instances' ,
@@ -281,7 +282,7 @@ have security groups, you have to add an **Egress** rule to one Security Group,
281282and an ** Ingress** rule to the other. The connections object will automatically
282283take care of this for you:
283284
284- ``` ts
285+ ``` ts fixture=conns
285286// Allow connections from anywhere
286287loadBalancer .connections .allowFromAnyIpv4 (ec2 .Port .tcp (443 ), ' Allow inbound HTTPS' );
287288
@@ -296,23 +297,23 @@ appFleet.connections.allowTo(dbFleet, ec2.Port.tcp(443), 'App can call database'
296297
297298There are various classes that implement the connection peer part:
298299
299- ``` ts
300+ ``` ts fixture=conns
300301// Simple connection peers
301302let peer = ec2 .Peer .ipv4 (" 10.0.0.0/16" );
302- let peer = ec2 .Peer .anyIpv4 ();
303- let peer = ec2 .Peer .ipv6 (" ::0/0" );
304- let peer = ec2 .Peer .anyIpv6 ();
305- let peer = ec2 .Peer .prefixList (" pl-12345" );
306- fleet .connections .allowTo (peer , ec2 .Port .tcp (443 ), ' Allow outbound HTTPS' );
303+ peer = ec2 .Peer .anyIpv4 ();
304+ peer = ec2 .Peer .ipv6 (" ::0/0" );
305+ peer = ec2 .Peer .anyIpv6 ();
306+ peer = ec2 .Peer .prefixList (" pl-12345" );
307+ appFleet .connections .allowTo (peer , ec2 .Port .tcp (443 ), ' Allow outbound HTTPS' );
307308```
308309
309310Any object that has a security group can itself be used as a connection peer:
310311
311- ``` ts
312+ ``` ts fixture=conns
312313// These automatically create appropriate ingress and egress rules in both security groups
313314fleet1 .connections .allowTo (fleet2 , ec2 .Port .tcp (80 ), ' Allow between fleets' );
314315
315- fleet .connections .allowFromAnyIpv4 (ec2 .Port .tcp (80 ), ' Allow from load balancer' );
316+ appFleet .connections .allowFromAnyIpv4 (ec2 .Port .tcp (80 ), ' Allow from load balancer' );
316317```
317318
318319### Port Ranges
@@ -342,12 +343,12 @@ If the object you're calling the peering method on has a default port associated
342343
343344For example:
344345
345- ``` ts
346+ ``` ts fixture=conns
346347// Port implicit in listener
347348listener .connections .allowDefaultPortFromAnyIpv4 (' Allow public' );
348349
349350// Port implicit in peer
350- fleet .connections .allowDefaultPortTo (rdsDatabase , ' Fleet can access database' );
351+ appFleet .connections .allowDefaultPortTo (rdsDatabase , ' Fleet can access database' );
351352```
352353
353354## Machine Images (AMIs)
@@ -374,7 +375,7 @@ examples of things you might want to use:
374375Create your VPC with VPN connections by specifying the ` vpnConnections ` props (keys are construct ` id ` s):
375376
376377``` ts
377- const vpc = new ec2 .Vpc (stack , ' MyVpc' , {
378+ const vpc = new ec2 .Vpc (this , ' MyVpc' , {
378379 vpnConnections: {
379380 dynamic: { // Dynamic routing (BGP)
380381 ip: ' 1.2.3.4'
@@ -393,13 +394,13 @@ const vpc = new ec2.Vpc(stack, 'MyVpc', {
393394To create a VPC that can accept VPN connections, set ` vpnGateway ` to ` true ` :
394395
395396``` ts
396- const vpc = new ec2 .Vpc (stack , ' MyVpc' , {
397+ const vpc = new ec2 .Vpc (this , ' MyVpc' , {
397398 vpnGateway: true
398399});
399400```
400401
401402VPN connections can then be added:
402- ``` ts
403+ ``` ts fixture=with-vpc
403404vpc .addVpnConnection (' Dynamic' , {
404405 ip: ' 1.2.3.4'
405406});
@@ -408,9 +409,10 @@ vpc.addVpnConnection('Dynamic', {
408409Routes will be propagated on the route tables associated with the private subnets.
409410
410411VPN connections expose [ metrics (cloudwatch.Metric)] ( https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-cloudwatch/README.md ) across all tunnels in the account/region and per connection:
411- ``` ts
412+
413+ ``` ts fixture=with-vpc
412414// Across all tunnels in the account/region
413- const allDataOut = VpnConnection .metricAllTunnelDataOut ();
415+ const allDataOut = ec2 . VpnConnection .metricAllTunnelDataOut ();
414416
415417// For a specific vpn connection
416418const vpnConnection = vpc .addVpnConnection (' Dynamic' , {
@@ -431,8 +433,9 @@ By default, interface VPC endpoints create a new security group and traffic is *
431433automatically allowed from the VPC CIDR.
432434
433435Use the ` connections ` object to allow traffic to flow to the endpoint:
434- ``` ts
435- myEndpoint .connections .allowDefaultPortFrom (... );
436+
437+ ``` ts fixture=conns
438+ myEndpoint .connections .allowDefaultPortFromAnyIpv4 ();
436439```
437440
438441Alternatively, existing security groups can be used by specifying the ` securityGroups ` prop.
@@ -443,17 +446,17 @@ You can use bastion hosts using a standard SSH connection targetting port 22 on
443446feature of AWS Systems Manager Session Manager, which does not need an opened security group. (https://aws.amazon.com/about-aws/whats-new/2019/07/session-manager-launches-tunneling-support-for-ssh-and-scp/ )
444447
445448A default bastion host for use via SSM can be configured like:
446- ``` ts
449+ ``` ts fixture=with-vpc
447450const host = new ec2 .BastionHostLinux (this , ' BastionHost' , { vpc });
448451```
449452
450453If you want to connect from the internet using SSH, you need to place the host into a public subnet. You can then configure allowed source hosts.
451- ``` ts
454+ ``` ts fixture=with-vpc
452455const host = new ec2 .BastionHostLinux (this , ' BastionHost' , {
453456 vpc ,
454- subnetSelection: { subnetType: SubnetType .PUBLIC },
457+ subnetSelection: { subnetType: ec2 . SubnetType .PUBLIC },
455458});
456- host .allowSshAccessFrom (Peer .ipv4 (' 1.2.3.4/32' ));
459+ host .allowSshAccessFrom (ec2 . Peer .ipv4 (' 1.2.3.4/32' ));
457460```
458461
459462As there are no SSH public keys deployed on this machine, you need to use [ EC2 Instance Connect] ( https://aws.amazon.com/de/blogs/compute/new-using-amazon-ec2-instance-connect-for-ssh-access-to-your-ec2-instances/ )
0 commit comments