Sub topic 13
Security groups versus network ACLs
This chapter assumes you know CIDR notation, ports, and the basics of VPC subnets, and that you have read the earlier chapters on stateful, allow-only security group rules. Security groups are one of two firewall controls in a VPC. The other is the network access control list (network ACL, or NACL). They overlap, but they work differently, so you need to know which one fits a requirement.
Two layers, two scopes
A security group attaches to a network interface, such as the one on an EC2 instance. A network ACL attaches to a subnet and filters traffic as it enters or leaves that subnet. Every subnet is associated with exactly one network ACL. One network ACL can serve many subnets.
Traffic to an instance passes both checks. If either one denies it, the traffic is dropped.
The key differences
| Property | Security group | Network ACL |
|---|---|---|
| Attached to | Network interface | Subnet |
| Rule types | Allow only | Allow and deny |
| State | Stateful | Stateless |
| Rule evaluation | All rules are evaluated together | Rules are evaluated in number order, and the first match wins |
| Default for new resources | No inbound rules, all outbound allowed | Default NACL allows all traffic. A custom NACL denies all traffic |
| Source can be another group | Yes | No, only CIDR blocks |
Stateless means you write both directions
A security group remembers a connection, so return traffic is allowed automatically. A network ACL does not. It judges each packet on its own, so you must allow the request and the response.
Suppose clients connect to HTTPS on a server in a subnet. The server replies from port 443 to the client's source port. That source port is an ephemeral port, a temporary port the client's operating system picks at random. The range depends on the client OS. Linux commonly uses 32768-60999, and Windows uses 49152-65535. For internet clients, you usually allow the full range 1024-65535 in the opposite direction.
A custom NACL for a public web subnet therefore needs rules like these:
| Direction | Rule number | Protocol | Port range | Source or destination | Action |
|---|---|---|---|---|---|
| Inbound | 100 | TCP | 443 | 0.0.0.0/0 | Allow |
| Outbound | 100 | TCP | 1024-65535 | 0.0.0.0/0 | Allow |
| Both | * | All | All | 0.0.0.0/0 | Deny |
The final * rule is built in. It denies anything that no numbered rule matched. Leaving out the outbound ephemeral range is the most common NACL mistake. Connections then hang even though the security group looks correct.
Rule order matters in a NACL
NACL rules are numbered from 1 to 32766. AWS checks them from the lowest number up and stops at the first match. To block one address inside a range you otherwise allow, give the deny a lower number than the allow:
| Rule number | Action | Source | Result |
|---|---|---|---|
| 90 | Deny | 198.51.100.7/32 | Blocked first |
| 100 | Allow | 0.0.0.0/0 | Everything else passes |
Leave gaps between numbers, such as 100, 110, 120, so you can insert rules later.
Choosing the right control
Use security groups as your main control. They are easier to reason about, they can reference other groups, and they follow instances as they move between subnets.
Use a network ACL when you need something a security group cannot do:
- Explicitly deny a known bad IP address or range. A security group can only fail to allow it.
- Apply one coarse rule to every resource in a subnet, as a second layer behind the security groups.
Keep NACLs simple. The more numbered rules you add, the harder it is to diagnose why a packet was dropped.
Exercise
An instance has a security group that allows inbound TCP 443 from 0.0.0.0/0. Clients still time out. The subnet uses a custom NACL with an inbound allow for TCP 443 and no outbound rules other than the default deny. What is wrong, and how do you fix it?
Show answer
The NACL is stateless, so the server's replies are dropped on the way out. Add an outbound allow rule for TCP on the ephemeral port range, for example 1024-65535, to the clients' CIDR range. No security group change is needed.
Exercise
You must block a single abusive IP address, 198.51.100.7, from reaching every instance in a subnet. Which control do you use, and what must you watch for in rule numbering?
Show answer
Use a network ACL, because security groups cannot express a deny. Add an inbound deny rule for 198.51.100.7/32 with a number lower than any allow rule that would match it, since the lowest matching number wins.