Sub topic 13
Security groups versus network ACLs
This chapter assumes you know how security groups work: stateful, allow-only rules attached to network interfaces. It also assumes you know what a VPC and a subnet are, and that you can read CIDR notation and run AWS CLI commands. Here you will learn what a network ACL (NACL) adds, where it differs, and how to pick the right control for a requirement.
Two filters at two boundaries
A VPC has two built-in packet filters. A security group acts at the network interface (ENI) of an instance. A network ACL acts at the boundary of a subnet. Traffic from the internet to an instance passes both, and each one can stop it.
The two layers are independent. A packet needs permission from both, so the effective policy is the intersection of what the NACL allows and what the security group allows.
The core differences
| Property | Security group | Network ACL |
|---|---|---|
| Applies to | An ENI, so any resource with one | A subnet, so every ENI in it |
| State | Stateful: return traffic is automatic | Stateless: each direction is judged on its own |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules are checked together, with no order | Rules are checked in ascending rule-number order and the first match wins |
| Default for new objects | No inbound allowed, all outbound allowed | A custom NACL denies everything until you add rules |
| Sources | CIDR blocks, prefix lists, other security groups | CIDR blocks only |
| Association | Many groups per ENI (up to the quota) | Exactly one NACL per subnet, but one NACL can serve many subnets |
The most important consequences are explained below.
Stateless filtering and ephemeral ports
A security group remembers a connection, so a reply is allowed without a rule. A NACL keeps no such memory. For a web server in a public subnet, you need a rule for the request and another rule for the reply.
- Inbound: allow TCP 443 from the clients.
- Outbound: allow TCP on the clients' ephemeral ports back to the clients.
An ephemeral port is the temporary source port a client picks for a connection. The range depends on the client operating system. Linux commonly uses 32768–60999, and Windows uses 49152–65535. Elastic Load Balancing and NAT gateways use 1024–65535. Because you rarely know which clients will connect, the usual safe choice is to allow outbound TCP 1024–65535 in the reply direction.
The same applies in reverse. If an instance in the subnet calls out to an HTTPS API, the NACL needs an outbound rule for port 443. It also needs an inbound rule for the ephemeral port range so the response can come back in.
Rule numbers and first match wins
Each NACL rule has a number from 1 to 32766. AWS evaluates rules from the lowest number up and stops at the first rule that matches the packet. A final rule marked * denies anything that no numbered rule matched, and you cannot edit or delete it.
This order lets you write exceptions. Consider these inbound rules:
| Rule | Protocol | Port | Source | Action |
|---|---|---|---|---|
| 90 | TCP | 443 | 198.51.100.0/24 | DENY |
| 100 | TCP | 443 | 0.0.0.0/0 | ALLOW |
| * | all | all | 0.0.0.0/0 | DENY |
A client in 198.51.100.0/24 matches rule 90 first and is denied. Every other client falls through to rule 100 and is allowed. If you swapped the numbers, the allow rule would match first and the deny would never apply.
A common habit is to number rules in steps of 10 or 100. That leaves room to insert a rule between two existing ones later without renumbering.
Default and custom NACLs
Every subnet is associated with exactly one NACL. If you do not choose one, the subnet uses the VPC's default NACL, which allows all inbound and all outbound traffic. In that state the NACL does nothing, and the security group is your only filter.
A NACL you create yourself starts closed. It denies all traffic in both directions, so a subnet moved onto a new custom NACL goes dark until you add allow rules. Plan the rule set, including the ephemeral-port rules, before you change the association.
What NACLs do not filter
Some traffic bypasses NACLs. This includes requests to the Amazon DNS resolver, DHCP, the instance metadata service at 169.254.169.254, and the Amazon Time Sync Service. Traffic between instances inside the same subnet is also not evaluated by the subnet's NACL, because it does not cross the subnet boundary. Security groups still apply to that traffic. This matters if you hoped a NACL could isolate two instances that share a subnet. It cannot, and a security group has to do that job.
Creating NACL rules with the CLI
This example creates a NACL, then adds an inbound HTTPS allow, a deny for one abusive range placed before it, and the outbound ephemeral-port rule for replies. Replace the IDs with your own.
# Create the NACL in the VPC
aws ec2 create-network-acl --vpc-id vpc-0123456789abcdef0
# Inbound: deny one range first (lower number wins)
aws ec2 create-network-acl-entry \
--network-acl-id acl-0123456789abcdef0 \
--ingress \
--rule-number 90 \
--protocol tcp \
--port-range From=443,To=443 \
--cidr-block 198.51.100.0/24 \
--rule-action deny
# Inbound: allow HTTPS from everywhere else
aws ec2 create-network-acl-entry \
--network-acl-id acl-0123456789abcdef0 \
--ingress \
--rule-number 100 \
--protocol tcp \
--port-range From=443,To=443 \
--cidr-block 0.0.0.0/0 \
--rule-action allow
# Outbound: allow replies to client ephemeral ports
aws ec2 create-network-acl-entry \
--network-acl-id acl-0123456789abcdef0 \
--egress \
--rule-number 100 \
--protocol tcp \
--port-range From=1024,To=65535 \
--cidr-block 0.0.0.0/0 \
--rule-action allow
# Review the rules
aws ec2 describe-network-acls --network-acl-ids acl-0123456789abcdef0The --ingress and --egress flags choose the direction, and --rule-action takes allow or deny. Rule numbers are unique within a direction, so inbound 100 and outbound 100 can coexist. To put the NACL into use, associate it with a subnet using aws ec2 replace-network-acl-association, which swaps the subnet's current association for the new NACL.
Choosing the right control
Use security groups as your main control. They are easier to reason about, they follow the instance, and they can reference other security groups, which lets tiered designs survive IP changes and scaling. Reach for a NACL when you need something a security group cannot do.
- Explicit deny. Block a known-bad CIDR range at the subnet edge. A security group cannot express "everything except this range", because it can only allow.
- A guardrail for a whole subnet. Enforce a rule that holds even if someone loosens a security group by mistake. For example, deny inbound SSH from the internet for every subnet in a private tier.
- Separation of duties. A network team can own the NACLs while application teams own the security groups.
A NACL is a poor fit for fine-grained, per-application rules. With stateless matching, ephemeral ports, and a 20-rule default quota per direction, it gets hard to maintain. Keep NACL rule sets short, and keep the detailed policy in security groups.
Troubleshooting with both layers
When a connection fails, check both filters, because either one can be responsible.
- Inspect the security group on the destination ENI for an inbound allow, and on the source for an outbound allow.
- Find the NACL associated with each subnet involved.
- Check the inbound rules for the destination subnet and the outbound rules for the source subnet.
- Check the reverse direction too, including the ephemeral-port range, since the NACL does not track the connection.
- Look for a lower-numbered deny that matches before your allow.
A connection that works from inside the subnet but fails from outside, or that hangs after the first packet, points to a NACL. A failure with a clean NACL points back to the security group.
Exercise
A web server in a subnet with a custom NACL serves HTTPS. The NACL has an inbound allow for TCP 443 from 0.0.0.0/0, and the security group allows 443. Clients connect but every request times out. The outbound NACL rules are empty. What is wrong, and what rule fixes it?
Show answer
A custom NACL denies everything it has no rule for, and it is stateless. The inbound request is allowed, but the reply has no outbound allow rule, so the NACL drops it at the subnet boundary. Add an outbound rule allowing TCP on ports 1024–65535 to 0.0.0.0/0, which covers client ephemeral ports. The security group needs no change because it tracks the connection.
Exercise
You must block inbound HTTPS from 203.0.113.0/24 while allowing it from everywhere else. A NACL has an allow rule 100 for TCP 443 from 0.0.0.0/0. What number must the deny rule have, and why can't a security group do this?
Show answer
The deny rule needs a number lower than 100, for example 90, because NACL rules are evaluated in ascending order and the first match wins. A deny numbered above 100 would never be reached for this traffic. A security group cannot do this because it has only allow rules, so it can't express an exception to an allowed range.