Sub topic 8
Network ACLs: stateless subnet-level rules
This chapter assumes you know security groups, subnets, and route tables from earlier chapters. You should also know that TCP clients connect from a random high-numbered source port.
What a network ACL is
A network ACL (NACL) is a numbered list of allow and deny rules attached to a subnet. It filters traffic as it crosses the subnet boundary, before security groups see it. Every subnet has exactly one NACL. One NACL can cover many subnets.
NACLs differ from security groups in three ways:
| Feature | Security group | Network ACL |
|---|---|---|
| Scope | Network interface | Subnet |
| State | Stateful | Stateless |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules together | Lowest rule number first, stops at first match |
Stateless means two directions
A security group remembers a connection and lets the reply through. A NACL does not. You must allow the request in one direction and the reply in the other.
Suppose a web server in a subnet listens on TCP 443. Clients connect from ephemeral ports, which are temporary ports the operating system picks. So the NACL needs two rules:
- Inbound: allow TCP 443 from the client range.
- Outbound: allow TCP 1024-65535 to the client range, for the replies.
Forgetting the ephemeral range is the most common NACL mistake. The symptom is a connection that hangs, even though the security group looks correct.
Default and custom NACLs
The default NACL that comes with a VPC allows all inbound and outbound traffic. A NACL you create starts with no allow rules, so it denies everything until you add some. Every NACL also has a final rule marked * that denies anything unmatched. You cannot change or delete it.
Leave gaps between rule numbers, such as 100, 110, 120. That lets you insert a rule later without renumbering.
Build a custom NACL
This example assumes VPC_ID and PRIVATE_SUBNET_ID are set from the earlier chapters. It allows HTTPS in from the VPC range, allows replies out, and blocks one hostile address with a deny rule that sits ahead of the allow.
NACL_ID=$(aws ec2 create-network-acl --vpc-id "$VPC_ID" --query NetworkAcl.NetworkAclId --output text)
# Deny a specific address first (lowest number wins)
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --ingress --rule-number 90 \
--protocol tcp --port-range From=443,To=443 --cidr-block 203.0.113.7/32 --rule-action deny
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --ingress --rule-number 100 \
--protocol tcp --port-range From=443,To=443 --cidr-block 10.0.0.0/16 --rule-action allow
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --egress --rule-number 100 \
--protocol tcp --port-range From=1024,To=65535 --cidr-block 10.0.0.0/16 --rule-action allow
ASSOC_ID=$(aws ec2 describe-network-acls \
--filters "Name=association.subnet-id,Values=$PRIVATE_SUBNET_ID" \
--query "NetworkAcls[0].Associations[?SubnetId=='$PRIVATE_SUBNET_ID'].NetworkAclAssociationId" \
--output text)
aws ec2 replace-network-acl-association --association-id "$ASSOC_ID" --network-acl-id "$NACL_ID"Where NACLs fit
Use security groups for most access control. Use a NACL when you need one of these:
- An explicit deny, such as blocking a known bad CIDR range.
- A coarse guardrail that applies to every resource in a subnet, whatever its security groups say.
NACLs also apply to traffic between instances in the same subnet only if it leaves the subnet. Traffic inside one subnet is not filtered by the subnet's NACL.
Exercise
A web server in a subnet with a custom NACL accepts TCP 443 inbound in the NACL, and its security group allows 443. Clients still time out. What rule is likely missing, and why?
Show answer
An outbound NACL rule allowing TCP 1024-65535 to the client addresses. NACLs are stateless, so reply traffic is not allowed automatically. The reply leaves from port 443 to the client's ephemeral port, and the outbound rules must permit it.