Security Groups vs. NACLs

Difficulty: Intermediate

Question

What is the difference between a Security Group and a Network ACL (NACL) in an AWS VPC, and when would you use each?

Answer

A Security Group is a stateful virtual firewall attached to an elastic network interface (so effectively to an EC2 instance, RDS instance, Lambda in a VPC, or load balancer), and it only supports allow rules. A Network ACL is a stateless firewall attached to a subnet, it supports both allow and deny rules, and it evaluates numbered rules in order. In practice Security Groups do almost all the work, and NACLs are a coarse second layer at the subnet edge.

Stateful vs stateless is the real difference

"Stateful" means the Security Group remembers connections. If you allow inbound TCP 443 from the internet, the response traffic going back out is allowed automatically, regardless of outbound rules. The same is true in reverse: if the instance opens an outbound connection, the reply is let back in.

A NACL has no memory. Every packet is checked against the rules for its direction. So if you allow inbound 443 on a subnet NACL, you must also allow outbound traffic to the client's ephemeral port range (typically 1024-65535), or the responses are silently dropped. This is the single most common NACL bug: everything looks open, but connections hang.

Side-by-side comparison

AspectSecurity GroupNetwork ACL
Attached toNetwork interface (instance level)Subnet
StateStatefulStateless
Rule typesAllow onlyAllow and deny
EvaluationAll rules evaluated togetherLowest rule number first, first match wins
Default (new custom one)Deny all inbound, allow all outboundDeny everything until you add rules
Default VPC versionAllows traffic from members of the same groupAllows all inbound and outbound
Can referenceOther Security Groups, prefix lists, CIDRsCIDRs only

How a packet is actually checked

For traffic entering an instance from outside its subnet, the inbound NACL of the subnet is checked first, then the instance's Security Group. On the way out, the Security Group (or connection tracking) passes it, then the outbound NACL. Traffic between two instances in the same subnet does not cross a NACL at all, only Security Groups.

When to use each

  • Security Groups for everything day to day. The best pattern is referencing groups instead of IPs: the database group allows port 5432 only from the app-sg group. When the app tier autoscales, new instances are allowed automatically, with no IP lists to maintain.
  • NACLs for explicit denies and guardrails. Blocking a known abusive CIDR, or making sure a "data" subnet can never talk to the internet even if someone misconfigures a Security Group. Because you cannot write a deny rule in a Security Group, NACLs are the only native VPC tool for that (AWS WAF is the better choice for HTTP-level blocking).
  • Many teams leave NACLs as the default allow-all and rely on Security Groups. That is a valid choice, not a mistake, as long as Security Groups are tight.

Common gotchas

  • Forgetting ephemeral ports in outbound NACL rules, as described above.
  • Leaving a gap in NACL rule numbers is good practice (100, 110, 120) so you can insert rules later.
  • Removing a Security Group rule does not always cut connections that are already tracked; an established session can continue until it closes. A NACL deny takes effect on the next packet.
  • Opening 0.0.0.0/0 on port 22 is what security audits flag first. Use Systems Manager Session Manager instead of SSH where possible.
Inbound packet path into an EC2 instance. The subnet NACL is checked before the instance Security Group, and NACLs need explicit return rules.
  1. Client on the internet: Sends a TCP SYN to port 443.
  2. Internet Gateway: Routes the packet into the VPC.
  3. Subnet NACL (inbound rules): Stateless check, first matching rule number wins.
  4. Security Group (inbound rules): Stateful allow list on the network interface.
  5. EC2 instance: Replies leave via SG tracking, then the outbound NACL.

Code examples

Security Group that references another group

# App tier group: HTTPS from the ALB only
aws ec2 authorize-security-group-ingress \
  --group-id sg-0app1234 \
  --protocol tcp --port 443 \
  --source-group sg-0alb5678

# DB tier group: Postgres from the app tier only
aws ec2 authorize-security-group-ingress \
  --group-id sg-0db9999 \
  --protocol tcp --port 5432 \
  --source-group sg-0app1234

Referencing groups instead of IP ranges means newly launched app instances get database access automatically, and nothing else does.

NACL rules that need an explicit return path

# Deny a known bad range first (lower number = evaluated first)
aws ec2 create-network-acl-entry --network-acl-id acl-0abc \
  --ingress --rule-number 90 --protocol -1 \
  --cidr-block 203.0.113.0/24 --rule-action deny

# Allow inbound HTTPS
aws ec2 create-network-acl-entry --network-acl-id acl-0abc \
  --ingress --rule-number 100 --protocol tcp --port-range From=443,To=443 \
  --cidr-block 0.0.0.0/0 --rule-action allow

# Allow responses back out to client ephemeral ports (stateless!)
aws ec2 create-network-acl-entry --network-acl-id acl-0abc \
  --egress --rule-number 100 --protocol tcp --port-range From=1024,To=65535 \
  --cidr-block 0.0.0.0/0 --rule-action allow

Without the egress rule for ephemeral ports, inbound HTTPS would be accepted but every response would be dropped.

Key points

  • Security Group: stateful, instance (ENI) level, allow rules only, all rules evaluated
  • NACL: stateless, subnet level, allow and deny, numbered rules with first match wins
  • Stateless NACLs need outbound rules for ephemeral ports 1024-65535
  • Security Groups can reference other Security Groups; NACLs only understand CIDRs
  • Traffic within the same subnet is not filtered by the NACL

Common follow-up questions

  • Why do you need to open ephemeral ports in a NACL but not in a Security Group?
  • How would you block a single malicious IP address that is hitting your web servers?
  • How would you design Security Groups for a three-tier app with an ALB, app servers, and RDS?
  • What is the default behaviour of a newly created custom NACL?

Interview tips

  • Lead with stateful vs stateless and instance vs subnet, then immediately give the ephemeral port example; it proves you understand what stateless means
  • Mention Security Group referencing (sg-to-sg rules) as the real-world best practice
  • If asked how to block an IP, say NACL deny or AWS WAF, and explain why a Security Group cannot do it

Concepts covered

Security Group, Network ACL, Stateful vs Stateless, Ephemeral Ports, VPC Subnet

More AWS & Cloud Fundamentals questions