Catalog My learning Blog
AWS Tutorial for Developers: From Account to Production · Phase 2 · Lesson 10 · 3 min read

AWS Security Groups vs Network ACLs

On this page
  1. Security groups vs network ACLs compared
  2. Security groups that reference each other
  3. Network ACLs and ephemeral ports
  4. Debugging blocked VPC traffic
  5. Check yourself

Security groups vs network ACLs compared

AWS gives you two packet filters inside a VPC. New users often configure both the same way and then can't work out why traffic is blocked. They behave differently, and they're meant for different things.

Security groupNetwork ACL
Applies toNetwork interfaces (instances, load balancers, databases)Whole subnets
StateStateful: replies are allowed automaticallyStateless: replies need their own rule
RulesAllow onlyAllow and deny
EvaluationAll rules togetherIn order by rule number; first match wins
DefaultDeny all inbound, allow all outboundThe default NACL allows everything
SourcesIP ranges, prefix lists or other security groupsIP ranges only
Security groups are stateful and attach to resources; network ACLs are stateless, numbered rules applied at the subnet boundary.
Security groups are stateful and attach to resources; network ACLs are stateless, numbered rules applied at the subnet boundary.

Security groups that reference each other

Build your tiers with security groups that reference each other:

  • alb-sg: allow 443 from 0.0.0.0/0.
  • app-sg: allow 8080 from alb-sg.
  • db-sg: allow 5432 from app-sg.

Referencing a group means "any network interface in that group", so the rules keep working as instances are replaced and scale out. Run against a local AWS emulator:

Code
ALBSG=$(aws ec2 create-security-group --group-name alb-sg --description "ALB" \
  --vpc-id $VPC --query GroupId --output text)
APPSG=$(aws ec2 create-security-group --group-name app-sg --description "App servers" \
  --vpc-id $VPC --query GroupId --output text)

aws ec2 authorize-security-group-ingress --group-id $ALBSG --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $APPSG --protocol tcp --port 8080 --source-group $ALBSG
Code
True
tcp	8080	sg-74d81c533ee1849be

Because security groups are stateful, the app servers' responses flow back to the load balancer without an outbound rule.

Network ACLs and ephemeral ports

Use NACLs sparingly, for things security groups can't express:

  • Deny specific ranges (a known abusive network) for a whole subnet.
  • A hard boundary between subnet tiers as defense in depth.

Rules are evaluated lowest number first, and the first match decides:

Shell
aws ec2 create-network-acl-entry --network-acl-id $ACL --ingress \
  --rule-number 100 --protocol -1 --rule-action deny --cidr-block 198.51.100.0/24
aws ec2 create-network-acl-entry --network-acl-id $ACL --ingress \
  --rule-number 200 --protocol -1 --rule-action allow --cidr-block 0.0.0.0/0
aws ec2 create-network-acl-entry --network-acl-id $ACL --egress \
  --rule-number 200 --protocol -1 --rule-action allow --cidr-block 0.0.0.0/0
Code
100	False	deny	198.51.100.0/24
200	False	allow	0.0.0.0/0
200	True	allow	0.0.0.0/0

The egress rule matters. NACLs are stateless, so responses leaving the subnet need an outbound allow, and responses to clients go to ephemeral ports (1024 to 65535). Forgetting this is the classic NACL outage: requests arrive, replies are dropped, connections hang. A custom NACL denies everything until you add rules.

Here's why the ephemeral port range is needed, with real numbers. A client at 203.0.113.7 connects from its port 52344 to your server's port 443. The request matches an inbound rule for 443. The reply travels from port 443 back to 203.0.113.7:52344, and the NACL, which remembers nothing, judges it as a brand-new packet: it needs an outbound rule allowing destination port 52344. The next client picks a different port, so in practice the rule must allow the whole 1024 to 65535 range. A security group in the same position would recognize the reply as part of a connection it already allowed and let it out without any rule.

Debugging blocked VPC traffic

  1. Check the instance's security group inbound rule for the port and source.
  2. Check the subnet's NACL in both directions, including ephemeral ports.
  3. Check route tables (Day 9).
  4. Use VPC Reachability Analyzer to trace a path, and VPC Flow Logs to see ACCEPT or REJECT records.

Check yourself

Security groups are stateful: once an inbound connection is allowed, its response traffic is allowed automatically in the other direction, regardless of outbound rules.

With a network ACL deny rule numbered lower than the subnet's allow rules, since NACL rules are evaluated in number order. Security groups can't express a deny, only allows. For HTTP traffic, AWS WAF is another option.

NACLs are stateless, so responses need their own outbound rule. Without an outbound allow for the clients' ephemeral ports, typically 1024 to 65535, responses are dropped on the way out.

Responses go back to the client's ephemeral port, such as 52344, not to port 443. The stateless NACL needs an outbound rule allowing ports 1024 to 65535.