AWS & Modern Cloud Architecture · Chapter 4 · ~20 min · Intermediate
Security Groups vs. Network ACLs (NACLs)
Build defense-in-depth network security in AWS. Master the critical differences between stateful instance firewalls (Security Groups) and stateless subnet perimeters (Network ACLs), rule evaluation orders, and ephemeral port tracking.
- Differentiate stateful Security Groups from stateless Network ACLs (NACLs)
- Understand NACL numerical evaluation order (100, 200, *) and explicit Deny rules
- Configure outbound ephemeral return ports (1024-65535) for stateless traffic
- Implement layered defense-in-depth combining instance firewalls and subnet perimeters
Security Groups vs. Network ACLs (NACLs): learn it visually
Configure multi-tier cloud firewalls: stateful Security Groups at the ENI level vs stateless numbered Network ACLs at the subnet perimeter, ephemeral return ports, and security group rule chaining.
1Defense in Depth: Instance vs. Subnet Firewalls
In AWS VPC networking, a packet traveling from the internet to your virtual machine must pass through two separate firewall perimeters:
- Perimeter 1: Network Access Control List (NACL): Operates at the subnet boundary. All packets entering or leaving the subnet are filtered here first.
- Perimeter 2: Security Group (SG): Operates at the Elastic Network Interface (ENI) attached directly to your EC2 instance, ECS task, or RDS database.
The Definitive Architectural Comparison:
| Feature | Security Group | Network ACL (NACL) |
|---|---|---|
| Enforcement Layer | Instance ENI level | Subnet boundary level |
| State Mechanism | STATEFUL (Tracks connections) | STATELESS (Inspects each packet) |
| Supported Actions | ALLOW rules only | ALLOW and DENY rules |
| Rule Evaluation | All rules evaluated together | Ascending order (100, 200, *) |
Wrong idea: Expecting return packets to automatically pass through all firewalls
Assuming that if a client establishes an inbound connection on port 443, any firewall in the cloud will automatically allow the server's reply back out.
Real system: Security Groups are stateful; Network ACLs are strictly stateless
Security Groups track connection state in memory so return traffic passes automatically. NACLs are stateless: you MUST explicitly allow outbound ephemeral ports (1024-65535).
A client connects to an EC2 web server on TCP port 443. The Security Group allows inbound port 443, but has NO outbound rules. What happens to the return traffic?
2The Ephemeral Port Trap: Why Stateless NACLs Fail
When you open a web browser and visit https://example.com, your browser connects to the server on port 443. However, your local computer's operating system does not listen on port 443 for the response!
Instead, your client OS opens a random, temporary high-numbered port called an ephemeral port (typically in the range 1024-65535). The server's response packet is addressed to:
Because Network ACLs are stateless, they have no memory of the inbound connection. If your NACL does NOT have an outbound rule permitting traffic to ephemeral ports 1024-65535, the subnet perimeter drops the server's reply!
3NACL Rule Evaluation Order: Numbering & Explicit Deny
NACL rules are evaluated in ascending numerical order from lowest to highest. When a packet matches a rule's protocol, port, and CIDR block, AWS takes that action immediately and stops evaluating:
{
"InboundNaclRules": [
{ "Rule": 50, "Action": "DENY", "Protocol": "tcp", "Port": 22, "CIDR": "198.51.100.42/32", "Desc": "Block attacking IP" },
{ "Rule": 100, "Action": "ALLOW", "Protocol": "tcp", "Port": 443, "CIDR": "0.0.0.0/0", "Desc": "Allow public HTTPS" },
{ "Rule": 110, "Action": "ALLOW", "Protocol": "tcp", "Port": 80, "CIDR": "0.0.0.0/0", "Desc": "Allow public HTTP" },
{ "Rule": "*", "Action": "DENY", "Protocol": "all", "Port": "*", "CIDR": "0.0.0.0/0", "Desc": "Default fallback deny" }
]
}Predict first
You add Rule 50: DENY IP 198.51.100.42 on Port 443. Rule 100 allows Port 443 from 0.0.0.0/0. What happens when IP 198.51.100.42 sends an HTTPS request?
A company detects a distributed denial-of-service attack coming from an entire CIDR subnet (203.0.113.0/24). How should they block it in AWS?
4Hands-On: Test Stateful vs. Stateless Firewall Packet Flow
Simulate both inbound client requests and outbound server return responses below. Observe how stateful Security Groups track sessions in memory while stateless NACLs require outbound ephemeral rules:
Stateful vs. Stateless Firewall Simulator
NACL Rule 100: ALLOW TCP 443 matched from 0.0.0.0/0. Passed to instance ENI.
Security Group Inbound Rule matched: Allow TCP 443. Connection state recorded in memory.
5Security Group Chaining: Referencing SG IDs Instead of CIDRs
One of the most powerful features of AWS Security Groups is Security Group Chaining. Instead of hardcoding IP addresses or CIDR blocks into your backend database firewall, you reference the Security Group ID of the application tier:
{
"DatabaseSecurityGroup": {
"InboundRules": [
{
"Protocol": "tcp",
"Port": 5432,
"SourceSecurityGroupId": "sg-01234567webservers",
"Description": "Only allow PostgreSQL from instances bearing the web-tier SG"
}
]
}
}Now, whenever your web tier scales up from 2 instances to 50 instances via Auto Scaling, every new web instance can immediately connect to the database without needing its IP manually added to the database firewall!
Why is referencing a Security Group ID (sg-xxxx) in an inbound rule superior to hardcoding private IP addresses?
6Bug Hunt: Fix the Stateless NACL Outbound Ephemeral Port Range
A network engineer created a custom NACL for an internal web application. Inbound HTTPS traffic is accepted, but users report that web pages hang indefinitely and time out. Find and fix the stateless return port flaw below:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Keep inbound rule allowing port
443-443. - Clients listen on dynamic ephemeral return ports: change outbound PortRange to
1024-65535. - Ensure the outbound rule action is
allow.
7Chapter Knowledge Challenge
Test your understanding of security groups, NACLs, statefulness, and ephemeral ports:
Why do stateless Network ACLs drop return HTTP/HTTPS responses if the outbound ephemeral port range (1024-65535) is omitted?
8Recap
- Security Groups are stateful firewalls at the instance ENI layer; return packets pass automatically.
- Network ACLs (NACLs) are stateless firewalls at the subnet perimeter; return packets require explicit outbound rules.
- Ephemeral Ports (1024-65535): Stateless NACLs must permit outbound traffic to ephemeral ports for client communications.
- Explicit Deny: Only NACLs support explicit Deny rules for blocking malicious IP addresses.
- Security Group Chaining: Authorize traffic by referencing Security Group IDs for seamless Auto Scaling integration.
Ready for Chapter 5 (EC2 Auto Scaling & ALBs)? Mark this chapter as complete to proceed: