Difficulty: Intermediate
What is the difference between a Security Group and a Network ACL (NACL) in an AWS VPC, and when would you use each?
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" 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.
| Aspect | Security Group | Network ACL |
|---|---|---|
| Attached to | Network interface (instance level) | Subnet |
| State | Stateful | Stateless |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules evaluated together | Lowest rule number first, first match wins |
| Default (new custom one) | Deny all inbound, allow all outbound | Deny everything until you add rules |
| Default VPC version | Allows traffic from members of the same group | Allows all inbound and outbound |
| Can reference | Other Security Groups, prefix lists, CIDRs | CIDRs only |
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.
app-sg group. When the app tier autoscales, new instances are allowed automatically, with no IP lists to maintain.0.0.0.0/0 on port 22 is what security audits flag first. Use Systems Manager Session Manager instead of SSH where possible.# 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.
# 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.
Security Group, Network ACL, Stateful vs Stateless, Ephemeral Ports, VPC Subnet