Sub topic 8
Network ACLs: stateless subnet-level rules
This chapter assumes you have read the earlier chapters on subnets, route tables, NAT gateways, and security groups. You should also know TCP ports and the difference between a source port and a destination port.
A network ACL (NACL) is a numbered list of allow and deny rules attached to a subnet. It filters every packet that enters or leaves that subnet. Security groups protect individual network interfaces. A NACL protects the whole subnet boundary, and it is stateless, so it keeps no memory of earlier packets.
How a NACL evaluates traffic
Each NACL has two independent rule lists, one for inbound (ingress) and one for outbound (egress). Every rule has:
- A rule number from 1 to 32766.
- A protocol (
tcp,udp,icmp, or-1for all). - A port range, for TCP and UDP.
- A source CIDR for inbound rules, or a destination CIDR for outbound rules.
- An action, either
allowordeny.
AWS checks rules in ascending number order and stops at the first match. Every list also ends with an unnumbered rule, shown as *, that denies anything no numbered rule matched. You cannot edit or remove this rule.
Because the first match wins, order matters. To block one address inside a range you otherwise allow, give the deny rule a lower number than the allow rule. If the deny has the higher number, the allow matches first and the deny is never reached.
Number rules in steps of 10 or 100 (100, 110, 120). The gaps let you insert a rule later without renumbering.
Default and custom NACLs
Every VPC comes with a default NACL. It allows all inbound and all outbound traffic, so it never blocks anything. Every subnet that you don't explicitly associate with another NACL uses it.
A custom NACL starts with only the * deny rules. A subnet associated with an empty custom NACL has no connectivity at all until you add allow rules.
Some facts about associations:
- A subnet is associated with exactly one NACL at a time.
- One NACL can be associated with many subnets.
- A NACL cannot reference security groups. It matches only on CIDR blocks, protocols, and ports.
- By default, each direction of a NACL can hold 20 rules. The quota is adjustable.
Stateless filtering and ephemeral ports
A security group is stateful. If it allows a request in, the reply is allowed out automatically. A NACL does not do this. It judges every packet on its own, so you must allow the request and the reply separately.
Suppose a client connects to a service on TCP 8080. The request goes to destination port 8080. The reply goes back from port 8080 to the client's ephemeral port. The client's operating system picks the ephemeral port from a high range, such as 32768–60999 on many Linux systems and 49152–65535 on Windows. A NACL that allows inbound 8080 but has no outbound rule for high ports will drop every reply. The connection then hangs until it times out.
For this reason, rules usually come in pairs:
| Direction of the request | Rule needed on the server's subnet | Rule needed on the client's subnet |
|---|---|---|
| Client to server on TCP 8080 | Inbound allow TCP 8080 | Outbound allow TCP 8080 |
| Server reply to client | Outbound allow TCP 1024–65535 | Inbound allow TCP 1024–65535 |
When the client and server are in different subnets, both subnets' NACLs apply. Traffic leaving the client's subnet passes its outbound rules, and traffic entering the server's subnet passes its inbound rules. The reply makes the same trip in reverse. Traffic between two instances in the same subnet never crosses a subnet boundary, so the NACL does not filter it.
A common way to cover the reply is to allow TCP 1024–65535. That range includes the Linux and Windows defaults, plus the range that NAT gateways and load balancers use.
Building a NACL for the private subnet
This example builds on the $VPC_ID, $PRIVATE_SUBNET_ID, and 10.0.1.0/24 public subnet from the earlier chapters. The private subnet hosts an app listening on TCP 8080. It accepts requests only from the public tier, and it makes outbound HTTPS calls through the NAT gateway.
NACL_ID=$(aws ec2 create-network-acl --vpc-id "$VPC_ID" \
--query NetworkAcl.NetworkAclId --output text)
# Inbound: app traffic from the public subnet
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --ingress \
--rule-number 100 --protocol tcp --port-range From=8080,To=8080 \
--cidr-block 10.0.1.0/24 --rule-action allow
# Inbound: replies to our own outbound HTTPS requests (via NAT)
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --ingress \
--rule-number 110 --protocol tcp --port-range From=1024,To=65535 \
--cidr-block 0.0.0.0/0 --rule-action allow
# Outbound: replies to the public subnet
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.1.0/24 --rule-action allow
# Outbound: HTTPS to the internet through the NAT gateway
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --egress \
--rule-number 110 --protocol tcp --port-range From=443,To=443 \
--cidr-block 0.0.0.0/0 --rule-action allow
# Find the subnet's current association with the default NACL, then swap it
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 | [0]" \
--output text)
aws ec2 replace-network-acl-association \
--association-id "$ASSOC_ID" --network-acl-id "$NACL_ID"Rule 110 inbound allows high ports from any address, because the NAT gateway forwards replies that originate from arbitrary internet hosts. The route table still decides what can reach the subnet, and the security groups on the instances still apply. If you know the specific services you call, you can narrow the source CIDR.
Rules take effect within moments, with no restart. The new rules also affect existing connections. If you remove a rule that a live connection depends on, that connection breaks.
Blocking an address
NACLs are the only VPC control that can explicitly deny. Security groups have allow rules only. To block a known-bad range on a public subnet, add a low-numbered deny rule:
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" --ingress \
--rule-number 50 --protocol -1 --cidr-block 203.0.113.0/24 --rule-action denyRule 50 sits before the allow rules at 100 and above, so it matches first. Protocol -1 means all protocols, and you must omit --port-range with it.
Traffic that NACLs do not filter
Some traffic never passes through NACL rules. The main cases:
- Requests to the Amazon DNS resolver (the VPC base address plus two).
- DHCP traffic.
- Requests to the instance metadata service at 169.254.169.254.
- Requests to the Amazon Time Sync Service at 169.254.169.123.
- Windows license activation traffic.
- Traffic to the reserved address used by the default VPC router.
Your DNS lookups, then, work even when a custom NACL has no rule for port 53. Lookups sent to a DNS server you run yourself are ordinary traffic and do need rules.
Security groups compared with NACLs
| Property | Security group | Network ACL |
|---|---|---|
| Attached to | Network interface | Subnet |
| State | Stateful | Stateless |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules together | Lowest number first, first match wins |
| Match on | CIDR, security group, prefix list | CIDR only |
| Default for new ones | Deny all inbound, allow all outbound | Custom: deny all both ways |
In practice, put your fine-grained access control in security groups. They are easier to reason about, and they follow the instance when it moves. Use a NACL as a coarse second layer. Typical uses are blocking known-bad CIDR ranges, enforcing a rule that the database subnet accepts traffic only from the app subnets, and satisfying compliance requirements for a subnet-level control.
Many deployments keep the default NACL's allow-all behavior and rely on security groups. That is a valid choice. A complex custom NACL adds failure modes, and you should add one only when you need what it offers.
Troubleshooting
The most common NACL errors:
- Missing ephemeral-port rule. Requests arrive but replies vanish. Check that the return direction allows high ports.
- Wrong rule order. A broad allow at a low number shadows a narrower deny that has a higher number.
- Forgotten IPv6 entries. IPv4 works and IPv6 fails.
- A rule on only one subnet. Traffic that crosses two subnets needs rules in both NACLs.
- Assuming the NACL is attached to the instance. Moving an instance to another subnet changes which NACL applies.
Exercise
A web server in subnet A listens on TCP 443. Subnet A uses a custom NACL with only this inbound rule: 100, TCP, port 443, source 0.0.0.0/0, allow. The outbound list has only the * deny rule. The security group allows inbound 443. Why do clients time out, and what is the smallest change that fixes it?
Show answer
The NACL is stateless, so the inbound allow does not permit the reply. The reply leaves from port 443 to the client's ephemeral port, and the outbound * rule denies it. Add an outbound rule, such as rule 100, TCP, ports 1024–65535, destination 0.0.0.0/0, allow. You could narrow the destination to the client range if it is known.