Skip to content

Commit 811dfe1

Browse files
committed
docs(ec2): document Network ACL usage and default NACL workaround
The default Network ACL created with a VPC allows all inbound and outbound traffic (rule 100), and CDK does not manage it. Users cannot remove or modify these permissive rules through CDK. Add a Network ACLs section to the README documenting: - How to create a custom Network ACL and associate it with subnets - How to work around the default NACL's permissive rules by replacing it with a custom NACL that only allows specific traffic Closes #13220
1 parent 09e3daf commit 811dfe1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

packages/aws-cdk-lib/aws-ec2/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,81 @@ new ec2.Vpc(this, 'VPC', {
713713
If you set this property to `true` and then later remove it or set it to `false`
714714
the default ingress/egress will be restored on the default security group.
715715

716+
### Network ACLs
717+
718+
You can create a custom Network ACL and associate it with specific subnets:
719+
720+
```ts
721+
declare const vpc: ec2.Vpc;
722+
723+
const nacl = new ec2.NetworkAcl(this, 'MyNACL', {
724+
vpc,
725+
subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
726+
});
727+
728+
nacl.addEntry('AllowHTTPSIngress', {
729+
cidr: ec2.AclCidr.anyIpv4(),
730+
ruleNumber: 100,
731+
traffic: ec2.AclTraffic.tcpPort(443),
732+
direction: ec2.TrafficDirection.INGRESS,
733+
ruleAction: ec2.Action.ALLOW,
734+
});
735+
736+
nacl.addEntry('AllowAllEgress', {
737+
cidr: ec2.AclCidr.anyIpv4(),
738+
ruleNumber: 100,
739+
traffic: ec2.AclTraffic.allTraffic(),
740+
direction: ec2.TrafficDirection.EGRESS,
741+
ruleAction: ec2.Action.ALLOW,
742+
});
743+
```
744+
745+
When a VPC is created, AWS automatically creates a [default Network ACL](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html#default-network-acl)
746+
that allows all inbound and outbound traffic (rule 100). CDK does not manage the default Network ACL,
747+
so its permissive rules cannot be removed or modified through CDK.
748+
749+
To restrict traffic at the Network ACL level, create a custom Network ACL and associate it with
750+
the desired subnets. When a custom Network ACL is associated with a subnet, it replaces the default
751+
Network ACL for that subnet. Custom Network ACLs deny all traffic by default, so you must
752+
explicitly add entries for the traffic you want to allow:
753+
754+
```ts
755+
declare const vpc: ec2.Vpc;
756+
757+
// Create a custom NACL that replaces the default permissive NACL
758+
const restrictiveNacl = new ec2.NetworkAcl(this, 'RestrictiveNACL', {
759+
vpc,
760+
subnetSelection: { subnetType: ec2.SubnetType.PUBLIC },
761+
});
762+
763+
// Only allow HTTPS inbound
764+
restrictiveNacl.addEntry('AllowHTTPS', {
765+
cidr: ec2.AclCidr.anyIpv4(),
766+
ruleNumber: 100,
767+
traffic: ec2.AclTraffic.tcpPort(443),
768+
direction: ec2.TrafficDirection.INGRESS,
769+
ruleAction: ec2.Action.ALLOW,
770+
});
771+
772+
// Allow ephemeral ports for return traffic
773+
restrictiveNacl.addEntry('AllowEphemeral', {
774+
cidr: ec2.AclCidr.anyIpv4(),
775+
ruleNumber: 200,
776+
traffic: ec2.AclTraffic.tcpPortRange(1024, 65535),
777+
direction: ec2.TrafficDirection.INGRESS,
778+
ruleAction: ec2.Action.ALLOW,
779+
});
780+
781+
// Allow all outbound
782+
restrictiveNacl.addEntry('AllowAllOutbound', {
783+
cidr: ec2.AclCidr.anyIpv4(),
784+
ruleNumber: 100,
785+
traffic: ec2.AclTraffic.allTraffic(),
786+
direction: ec2.TrafficDirection.EGRESS,
787+
ruleAction: ec2.Action.ALLOW,
788+
});
789+
```
790+
716791
## Allowing Connections
717792

718793
In AWS, all network traffic in and out of **Elastic Network Interfaces** (ENIs)

0 commit comments

Comments
 (0)