Filtering Traffic with Security Groups and Network ACLs
This lesson explains the two layers of firewall in a VPC and the differences between them that the exam regularly tests.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish stateful security groups from stateless network ACLs.
- Explain how security groups and network ACLs work together.
- Describe what VPC Flow Logs, Traffic Mirroring and Reachability Analyzer show.
Side by side
Security groups and network ACLs both filter traffic in a VPC, but they work at different levels and follow different logic. A security group is attached to network interfaces, so it protects individual resources, such as instances, load balancers, RDS databases, Lambda functions in a VPC and interface endpoints. It is stateful: if a request is allowed in, its response is allowed out automatically. Its rules can only allow traffic, and all rules are evaluated together. A new security group allows all outbound traffic and no inbound traffic. A rule can name another security group as its source, so a database can allow the application tier's group, whatever its instances' addresses.
A network ACL is attached to a subnet and filters all traffic crossing its boundary. It is stateless, so return traffic must be allowed by its own rule, usually on the ephemeral ports 1024 to 65535. Its rules can allow or deny, are evaluated in ascending order of rule number, and the first match wins. This makes network ACLs the tool for blocking a specific address range. The default network ACL allows everything, whereas a new custom ACL denies everything until rules are added. The following table summarises the differences.
The following diagram shows where each control applies to a request and its response.

Security group or network ACL?
Using them together
Security groups and network ACLs are used together, with different roles. Security groups are the primary control: they are stateful, attached to each resource, and can reference other security groups, so each tier admits traffic only from the tier in front of it, for example the database tier only from the application tier's security group.
Network ACLs are a coarse, subnet-level backstop. Because they are stateless and support deny rules, they suit broad blocks that security groups cannot express, such as denying a malicious IP range for an entire subnet. Because they are stateless, any NACL rule set must also allow return traffic on ephemeral ports.
Visibility and troubleshooting
Three tools show what happens to network traffic, at different levels of detail. VPC Flow Logs record metadata about each connection through a VPC, subnet or network interface: the source and destination addresses and ports, the protocol, the number of bytes and whether the traffic was accepted or rejected. They are delivered to CloudWatch Logs, S3 or Firehose and do not contain packet contents, so they answer whether traffic reached a resource and which rule type rejected it.
Traffic Mirroring copies the actual packets from a network interface to a monitoring appliance, for intrusion detection or deep troubleshooting that needs payload content. Reachability Analyzer analyses configuration rather than traffic: given a source and a destination, it reports whether a path exists and, if not, which route table, security group or network ACL blocks it, without sending any packets. The following table summarises the tools.
| Tool | Shows |
|---|---|
| VPC Flow Logs | Accepted and rejected traffic per VPC, subnet or ENI (to CloudWatch Logs, S3, Firehose); no packet contents |
| Traffic Mirroring | Packet contents |
| Reachability Analyzer | Which route table, security group or NACL blocks a path, without sending traffic |
Network visibility tools
Three tiers, least privilege
In a three-tier application, each tier should accept traffic only from the tier in front of it. The following table gives the inbound rule and permitted source for each security group.
| Security group | Inbound rule | Source |
|---|---|---|
| ALB | HTTPS 443 | 0.0.0.0/0 |
| Web tier | HTTP 8080 | The ALB security group |
| Database | PostgreSQL 5432 | The web tier security group |
Referencing security groups instead of IP ranges means the rules stay correct as instances scale in and out. When the company later needs to block a specific attacker's IP range, it adds a deny rule to the public subnets' network ACL, because security groups can only allow. Responses are allowed automatically by the stateful security groups, but a custom NACL must also allow outbound ephemeral ports, or replies will be dropped.
Which control can block traffic from one specific IP address range?
Show answer
A network ACL, because it supports deny rules. Security groups only allow traffic.
Exam Tips
Remember for the exam
- To block a particular IP address, use a network ACL deny rule, because security groups cannot deny.
- To allow the application tier only from the load balancer, reference the ALB's security group.
- Network ACLs are stateless, so remember outbound rules for ephemeral ports to allow responses.
- To investigate rejected connections, use VPC Flow Logs.
- To find out why instance A cannot reach instance B, use VPC Reachability Analyzer.