Every packet that reaches an EC2 instance in a VPC passes through up to two firewalls: the network ACL on its subnet and the security groups on its network interface. They look similar in the console but behave very differently, and confusing them is behind many “it works from here but not there” problems and many exposed servers.
This guide explains how each one works, when to use which, and a simple rule set for a typical web application.
How security groups work
A security group is a virtual firewall attached to a network interface, so it applies to an EC2 instance, a load balancer, an RDS database or a Lambda function in a VPC. It contains allow rules only, for inbound and outbound traffic, and everything not allowed is denied. A new security group allows all outbound traffic and no inbound traffic. The VPC security groups documentation lists the details and limits.
Security groups are stateful: when a request is allowed in, its response is allowed back out without a separate rule.

How network ACLs work
A network ACL applies to a whole subnet. It has numbered inbound and outbound rules that can allow or deny, evaluated from the lowest number up, and the first match wins. NACLs are stateless, so return traffic must be allowed explicitly. The default NACL in each VPC allows everything; a new custom NACL denies everything until you add rules. AWS documents them in the network ACL guide.
Side-by-side comparison
| Feature | Security group | Network ACL |
|---|---|---|
| Applies to | Network interface (instance, ELB, RDS) | Entire subnet |
| Rule types | Allow only | Allow and deny |
| State | Stateful | Stateless |
| Rule evaluation | All rules considered together | In number order, first match wins |
| Can reference other groups | Yes | No, CIDR ranges only |
| Default for new one | No inbound, all outbound | Custom NACL denies all |
Stateful vs stateless in practice
Imagine a web server that allows HTTPS on port 443. With a security group, one inbound rule for 443 is enough; responses go out automatically. With a custom NACL, you need an inbound rule for 443 and an outbound rule for the ephemeral ports the client uses for the reply, typically 1024 to 65535. Forgetting that outbound rule is the most common NACL mistake, and it silently breaks connections.
Rules for a typical web app
- Load balancer security group: inbound 443 and 80 from 0.0.0.0/0 (and ::/0 for IPv6).
- Web server security group: inbound application port only from the load balancer security group. No public SSH.
- Database security group: inbound 3306 or 5432 only from the web server security group.
- Admin access: Session Manager, so no inbound port is needed at all. The secure EC2 access guide shows the setup.
- NACLs: default allow, unless you have a specific reason to change them.
This layout pairs with public subnets for the load balancer and private subnets for servers and databases, as described in the public vs private subnets guide.
Referencing security groups
Instead of allowing an IP range, a rule can allow traffic from another security group. Any instance in that group is allowed, whatever its address. This keeps rules correct as Auto Scaling replaces instances and IPs change, and it documents intent: “the database accepts the web tier” is clearer than a list of addresses. The Auto Scaling and load balancer guide relies on this pattern.
When to use NACLs
- Blocking a specific abusive IP range for a whole subnet.
- Adding a coarse second layer for sensitive subnets, for example allowing a database subnet to talk only to the application subnets.
- Meeting a compliance requirement for subnet-level controls.
For web attacks, rate limiting and bot traffic, AWS WAF is usually a better tool than NACLs; the WAF and Shield guide explains why.
Troubleshooting blocked traffic
- Check the security group on the target for an inbound rule matching the port and source.
- Check the subnet’s NACL in both directions, including ephemeral ports.
- Check route tables, internet gateway or NAT, and the instance’s own firewall.
- Use VPC Reachability Analyzer to trace the path, or VPC Flow Logs to see ACCEPT and REJECT records.
Firewall checklist
- No inbound SSH, RDP or database ports open to 0.0.0.0/0.
- Tiers allowed by security group reference, not by IP.
- Unused security groups deleted; default security group has no rules in use.
- Custom NACLs include ephemeral port rules.
- Flow Logs enabled for important VPCs.
Firewall rules are one of the first things to review after taking over an account; the handover checklist covers it, and the details listed with each AWS account tell you what is already configured.
Security group and NACL questions
Are security groups stateful?
Yes. If a security group allows an inbound request, the response is allowed out automatically, and the reverse is also true for outbound requests. You do not need a matching rule for return traffic.
Can a security group deny traffic?
No. Security groups only have allow rules; anything not allowed is denied. To explicitly block an IP address or range, use a network ACL or AWS WAF for web traffic.
Why does my custom network ACL block everything?
A new custom network ACL denies all inbound and outbound traffic until you add rules. Because NACLs are stateless, you also need outbound rules for the ephemeral port range used by responses.
Should I open port 22 to the internet?
No. Use Session Manager or EC2 Instance Connect Endpoint for administration. If SSH must be open, limit it to specific trusted IP addresses.
How many security groups can an instance have?
By default, up to five security groups per network interface, and this quota can be raised. Each security group also has a limit on inbound and outbound rules.
Editorial guide by Mark Biden · Reviewed 24 November 2026. Official sources are linked beside the relevant sections; AWS features, quotas and prices change, so confirm details in the AWS console before relying on them.

Leave a Reply