I’ll cover:
-
What is Security Group (SG)
-
What is Network ACL (NACL)
-
Stateful vs Stateless (deep explanation)
-
Rule evaluation logic
-
Architecture placement in VPC
-
Real production scenarios
-
Interview-ready comparison table
🛡 1️⃣ Security Group (SG)
A Security Group is a virtual stateful firewall attached to ENIs (Elastic Network Interfaces).
That means:
-
It protects EC2 instances
-
It protects EKS worker nodes
-
It protects ALB/NLB
-
It protects RDS
-
Anything with ENI
🔹 Operates at:
OSI Layer 4 (TCP/UDP)
🔹 Applied at:
Instance level
🔥 Technical Behavior
-
Stateful
-
Default deny all inbound
-
Default allow all outbound
-
No explicit deny rules
-
Rules are evaluated as "allow-only"
🧠 Stateful Meaning (Important)
If inbound is allowed:
Example:
Allow inbound TCP 443 from 0.0.0.0/0
Return traffic is automatically allowed.
You DO NOT need to allow ephemeral ports.
Connection tracking is done automatically.
🌐 2️⃣ Network ACL (NACL)
A Network ACL is a stateless subnet-level firewall.
It controls traffic entering and leaving a subnet.
🔹 Operates at:
Layer 3 & 4 (IP + TCP/UDP)
🔹 Applied at:
Subnet level
🔥 Technical Behavior
-
Stateless
-
You MUST allow inbound AND outbound explicitly
-
Supports Allow AND Deny rules
-
Rules are processed in order (lowest number first)
🧠 Stateless Meaning
If inbound TCP 443 is allowed,
You must also allow outbound ephemeral ports (1024–65535).
Otherwise response traffic is dropped.
No connection tracking.
📍 Where They Sit in VPC
Traffic Flow:
Internet
↓
IGW
↓
NACL (Subnet boundary)
↓
Security Group (Instance boundary)
↓
EC2
Think:
NACL = outer perimeter firewall
SG = inner instance firewall
🧠 Rule Processing Difference (Critical)
Security Group
-
All rules evaluated together
-
If ANY rule matches → allow
-
Implicit deny at end
-
No rule order
-
No rule numbers
NACL
-
Rules evaluated in ascending rule number
-
First match wins
-
Explicit deny supported
-
Rule numbers mandatory
Example:
100 allow 443
110 deny 0.0.0.0/0
If rule 100 matches → rule 110 never checked.
🔥 Technical Comparison Table
| Feature | Security Group | NACL |
|---|---|---|
| Scope | ENI / Instance | Subnet |
| Stateful | Yes | No |
| Allow Rules | Yes | Yes |
| Deny Rules | No | Yes |
| Rule Order Matters | No | Yes |
| Default Behavior | Deny inbound | Allow all |
| Response Traffic | Auto allowed | Must allow manually |
| Layer | L4 | L3 + L4 |
| Common Usage | Application security | Network segmentation |
🚀 Real Production Scenarios
When to Use Security Groups
✔ Control microservice access
✔ EKS node security
✔ ALB → EC2 communication
✔ Database access control
Example:
Allow only app SG to talk to DB SG.
This is called SG referencing.
When to Use NACL
✔ Block malicious IP ranges
✔ Block entire country CIDRs
✔ Additional defense layer
✔ Compliance requirements
✔ DDoS mitigation layer
Example:
Deny known bad IP range at subnet level.
🎯 Important EKS Context (MLOps Angle)
In EKS:
-
Nodes have Security Groups
-
Pods may inherit node SG
-
LoadBalancers have SGs
-
NACL rarely modified unless compliance-heavy environment
Most production teams:
Use SG heavily
Keep NACL mostly default (unless strong segmentation required)
🧠 Deep Technical Insight (Interview Level)
Security Groups use:
👉 Connection tracking (state table)
👉 ENI attachment-level filtering
NACL uses:
👉 Stateless packet filtering
👉 Subnet boundary filtering
Because NACL is stateless:
It cannot inspect session state.
Because SG is stateful:
It maintains connection state in kernel-level networking stack.
🧠 Real-World Debugging Example
Problem:
EC2 not reachable on port 443.
Checklist:
-
Check Security Group inbound
-
Check NACL inbound
-
Check NACL outbound
-
Check route table
-
Check IGW
-
Check instance OS firewall
If SG correct but still blocked → NACL probably denying.
🏗 Production Best Practice
✔ Use Security Groups for fine-grained control
✔ Use NACL for coarse-grained subnet control
✔ Do NOT overcomplicate NACL unless required
✔ Avoid symmetric rule mistakes in NACL
🔥 Simple Mental Model
Security Group = Smart firewall (remembers conversations)
NACL = Dumb firewall (forgets everything)
🎯 Interview 30-Second Answer
"Security Groups are stateful, instance-level firewalls that only support allow rules and automatically permit return traffic. NACLs are stateless, subnet-level firewalls that require explicit inbound and outbound rules and support both allow and deny rules, processed in order."
Now we go deep networking engineer level so you understand:
-
What “stateless” really means
-
What “stateful” really means
-
What session state is
-
Where connection tracking happens
-
How packets actually flow in AWS
I’ll explain step-by-step + clear diagrams.
🧠 First: What Is “Session State”?
When a client connects to a server (example HTTPS):
Client → Server (TCP SYN)
Server → Client (SYN-ACK)
Client → Server (ACK)
This 3-way handshake creates a connection state.
After that, packets flow back and forth.
A firewall can either:
-
🔴 Remember this connection (stateful)
-
🔵 Forget everything (stateless)
🛡 PART 1 — Security Group (STATEFUL)
Security Groups are stateful.
That means:
✔ They track connection state
✔ They maintain a connection table
✔ Return traffic is automatically allowed
🔥 What “Maintains Connection State” Means
When you allow inbound:
Example rule:
Allow TCP 443 from 0.0.0.0/0
Now client connects:
Client IP: 1.2.3.4
Client Port: 53000 (ephemeral port)
Server Port: 443
SG sees:
Inbound TCP 443 → Allowed
Connection entry created in state table:
Now when server replies:
Outbound packet:
10.0.1.10:443 → 1.2.3.4:53000
SG checks:
Is this part of an existing session?
Yes → Automatically allow.
You DO NOT need an outbound rule for 53000.
That’s stateful behavior.
📊 Security Group Packet Flow Diagram
Inside SG:
🌐 PART 2 — NACL (STATELESS)
NACL is stateless.
That means:
❌ It does NOT remember connections
❌ It does NOT maintain session table
❌ Every packet evaluated independently
🔥 What “Cannot Inspect Session State” Means
Same HTTPS example:
Client → Server TCP 443
You allow inbound 443 in NACL.
Now server replies:
Server → Client (port 53000)
If outbound rule does NOT allow 1024–65535 range:
Packet is dropped.
Why?
Because NACL does NOT know:
“This is reply traffic.”
It just sees:
Outbound packet to 53000.
If not allowed explicitly → drop.
📊 NACL Packet Evaluation Diagram
Each direction is independent.
No memory.
🔬 Deep Technical Comparison
🔵 Stateless Firewall (NACL)
Works like:
No memory of previous packets.
Even part of same TCP connection is treated separately.
Think:
Security guard who forgets who entered 5 seconds ago.
🟢 Stateful Firewall (SG)
Works like:
Think:
Security guard with guest list + entry log.
🧠 OS-Level Understanding
Security Groups rely on:
✔ Connection tracking mechanism
✔ Stateful packet inspection
✔ Managed by AWS hypervisor layer
NACL works at:
✔ Subnet boundary
✔ Packet filtering before reaching ENI
✔ No session awareness
🔥 Real Packet Walkthrough (Full)
Client (1.2.3.4) connects to EC2 (10.0.1.10)
Step 1: Inbound packet hits NACL
Step 2: NACL checks inbound rules
Step 3: Forwarded to SG
Step 4: SG checks inbound rule
Step 5: SG creates connection state
Step 6: EC2 responds
Step 7: SG sees it’s established → allows
Step 8: NACL checks outbound rule
Step 9: Packet leaves subnet
📊 Complete Traffic Flow Diagram
🚨 Common Real-World Failure
Symptom:
Website not loading.
SG correct.
Cause:
NACL outbound missing ephemeral port range:
Since NACL is stateless, return traffic blocked.
🎯 Interview-Ready Deep Explanation
“NACL is stateless, meaning it evaluates each packet independently without maintaining session state. Therefore, both inbound and outbound rules must explicitly allow traffic. Security Groups are stateful; they maintain a connection tracking table, so return traffic is automatically permitted once an inbound connection is allowed.”
🧠 Ultra Simple Mental Model
Security Group = Smart firewall (remembers conversation)
NACL = Dumb firewall (forgets everything)
🔐 Security Group vs 🌐 Network ACL
(AWS VPC Deep Technical Comparison)
🛡 1️⃣ SECURITY GROUP (SG)
🔎 Definition
A Security Group is a stateful virtual firewall attached to ENIs (Elastic Network Interfaces).
It controls traffic at the instance level.
⚙ Technical Characteristics
-
✔ Stateful (connection tracking enabled)
-
✔ Attached to ENI / EC2 / ALB / RDS
-
✔ Supports only ALLOW rules
-
✔ No rule order (all rules evaluated together)
-
✔ Default: Deny all inbound, Allow all outbound
🧠 Stateful Meaning (Important)
If inbound traffic is allowed:
Return traffic is automatically allowed.
You DO NOT need to allow ephemeral ports manually.
Security Group maintains a connection state table.
📊 SECURITY GROUP DIAGRAM
🌐 2️⃣ NETWORK ACL (NACL)
🔎 Definition
A Network ACL is a stateless subnet-level firewall.
It controls traffic entering and leaving a subnet.
⚙ Technical Characteristics
-
✔ Stateless (no session tracking)
-
✔ Attached to Subnet
-
✔ Supports ALLOW and DENY rules
-
✔ Rules evaluated in order (lowest rule number first)
-
✔ Default: Allow all inbound & outbound (unless modified)
🧠 Stateless Meaning (Important)
Every packet is evaluated independently.
If inbound 443 is allowed:
Outbound ephemeral ports (1024–65535) must also be allowed manually.
Otherwise return traffic is dropped.
📊 NACL DIAGRAM
🔥 TECHNICAL DIFFERENCE TABLE
| Feature | Security Group | Network ACL |
|---|---|---|
| Level | Instance (ENI) | Subnet |
| Type | Stateful | Stateless |
| Connection Tracking | Yes | No |
| Allow Rules | Yes | Yes |
| Deny Rules | No | Yes |
| Rule Order | No order | Ordered evaluation |
| Default Inbound | Deny | Allow |
| Default Outbound | Allow | Allow |
| Return Traffic | Auto allowed | Must allow manually |
| Common Use | App-level control | Network segmentation |
🎯 Real Production Usage
Use Security Group When:
-
Controlling microservices access
-
ALB → EC2 communication
-
EKS node security
-
DB access control
Use NACL When:
-
Blocking malicious CIDR ranges
-
Compliance requirements
-
Extra network segmentation layer
-
Country-level blocking
🧠 Simple Mental Model
Security Group = Smart firewall (remembers sessions)
NACL = Strict gatekeeper (checks every packet separately)
We’ll simulate a real HTTPS request:
Client → EC2 (Port 443)
🎬 SCENE 1 — TRAFFIC ENTERS AWS
Packet enters your VPC.
🎬 SCENE 2 — NACL CHECK (Stateless Layer)
🧠 NACL Logic:
-
Check rule numbers in order
-
If 443 allowed → Forward
-
If not → Drop
NACL does NOT remember anything.
Packet passes.
🎬 SCENE 3 — SECURITY GROUP CHECK (Stateful Layer)
🧠 SG Logic:
-
Matches inbound rule
-
Creates connection entry in state table:
Packet forwarded to EC2.
🎬 SCENE 4 — EC2 RESPONDS
EC2 sends:
Now watch the difference.
🎬 SCENE 5 — SECURITY GROUP RETURN CHECK
Security Group sees:
Is this part of existing session?
✔ Yes → Allow automatically.
No need to open ephemeral ports.
That’s STATEFUL behavior.
🎬 SCENE 6 — NACL OUTBOUND CHECK
Now packet reaches NACL outbound rules:
If ephemeral ports allowed → Packet leaves.
If NOT allowed → Packet DROPPED.
That’s STATELESS behavior.
NACL does NOT know this is reply traffic.
🔄 COMPLETE ANIMATED FLOW
🎯 Visual Comparison Animation Style
🔵 If Using Only Security Group
Inbound 443 allowed
Return traffic automatically allowed
Works smoothly
🔴 If NACL Missing Ephemeral Rule
Inbound 443 allowed
EC2 responds
NACL blocks outbound 53000
Result:
❌ Connection fails
❌ Website not loading
This is a very common production mistake.
🧠 Deep Understanding Summary
| Feature | Security Group | NACL |
|---|---|---|
| Remembers session | ✔ Yes | ❌ No |
| Needs outbound rule for reply | ❌ No | ✔ Yes |
| Connection tracking | ✔ Yes | ❌ No |
| Rule order matters | ❌ No | ✔ Yes |
