NACLs vs security groups
Network ACLs as stateless subnet firewalls, rule numbers and order, the default NACL, ephemeral ports, and how NACLs differ from security groups.
How traffic is checked
Network ACLs
A network ACL (NACL) is like a firewall that controls traffic from and to subnets.
- One NACL per subnet; new subnets are assigned the default NACL.
- Each NACL holds NACL rules:
- rules have a number (1 to 32766); a lower number has higher precedence;
- the first rule that matches drives the decision;
- example: with
#100 ALLOW 10.0.0.10/32and#200 DENY 10.0.0.10/32, the IP is allowed, because 100 comes first; - the last rule is an asterisk (
*) that denies a request when no rule matches; - AWS recommends adding rules in increments of 100.
- Newly created custom NACLs deny everything.
- NACLs are a great way to block a specific IP address at the subnet level (security groups can't deny).
Default NACL
- Accepts everything inbound and outbound for the subnets it's associated with (rule
100ALLOW all, then*DENY). - Do NOT modify the default NACL; create custom NACLs instead.
Ephemeral ports
- For any two endpoints to establish a connection, they must use ports.
- Clients connect to a defined port (such as 443), and expect a response on an ephemeral port: a random high port the client OS picks.
- Different operating systems use different ranges:
- IANA and Windows 10: 49152–65535;
- many Linux kernels: 32768–60999.
NACL with ephemeral ports: for a web tier talking to a database on port 3306:
- Web subnet NACL: allow outbound TCP 3306 to the DB subnet CIDR; allow inbound TCP 1024–65535 from the DB subnet CIDR (the responses).
- DB subnet NACL: allow inbound TCP 3306 from the web subnet CIDR; allow outbound TCP 1024–65535 to the web subnet CIDR.
- Create NACL rules for each target subnet CIDR (for example, both web subnets in AZ A and AZ B).
Security group vs NACL
Security group:
- operates at the instance level (ENI);
- supports allow rules only;
- stateful: return traffic is automatically allowed, regardless of any rules;
- all rules are evaluated before deciding;
- applies to an EC2 instance when someone specifies it.
NACL:
- operates at the subnet level;
- supports allow and deny rules;
- stateless: return traffic must be explicitly allowed (think of ephemeral ports);
- rules are evaluated in order (lowest to highest); first match wins;
- automatically applies to all instances in the subnets it's associated with.
See also security groups.
Real-world examples
- A security team sees attacks from
198.51.100.23. Security groups can't deny, so they add NACL rule#50 DENY 198.51.100.23/32on the public subnets.
Exam tips
- "Block a specific IP address" → NACL deny rule (security groups can't deny).
- "Allowed inbound but the response never arrives" → NACL outbound rules missing the ephemeral port range.
- "Rule order" → NACLs evaluate lowest number first; security groups evaluate all rules.
- "Stateful vs stateless" → security groups stateful, NACLs stateless.
Common traps
- "A new custom NACL allows all traffic." It denies everything until rules are added; the default NACL allows all.
- "Deny rules in a security group." Only allow rules exist.
Check yourself
No. Rule #100 matches first and denies all SSH; rule #200 is never evaluated for that traffic. Give the allow rule a lower number than the deny.
AWS documentation
NAT instance and NAT gateway
How NAT gives private subnets outbound internet access, the outdated NAT instance, the managed NAT gateway and its high availability, the Regional NAT gateway, and the comparison.
VPC Flow Logs and Traffic Mirroring
VPC Flow Logs at the VPC, subnet and ENI level, their record format, troubleshooting security groups and NACLs with them, common architectures, and VPC Traffic Mirroring.