12.SecurityGroups
🛡️ AWS Security Groups: Firewall thông minh cho EC2 (Lý thuyết + thực chiến cho người mới)¶
PHẦN 1: Security Groups là gì?¶
Định nghĩa cơ bản¶
- Security Groups (SG) = stateful firewall ở cấp Network Interface (ENI)
- Attach vào ENI, không phải subnet hay instance
- Stateful → chỉ cần 1 rule, response tự động được allow
- Virtual firewall bao quanh từng ENI
Điểm khác biệt quan trọng với NACL¶
Security Groups vs NACL:
┌─────────────────────┬──────────────────┬─────────────────┐
│ Aspect │ Security Group │ NACL │
├─────────────────────┼──────────────────┼─────────────────┤
│ Level │ ENI/Instance │ Subnet │
│ State │ Stateful │ Stateless │
│ Rules needed │ 1 (auto response)│ 2 (in + out) │
│ Allow/Deny │ Allow only │ Both │
│ Rule processing │ All rules eval │ First match │
│ Logical references │ ✅ Yes │ ❌ No │
│ Ephemeral ports │ Auto handled │ Manual config │
└─────────────────────┴──────────────────┴─────────────────┘
PHẦN 2: Security Groups Key Features¶
1. Stateful Behavior¶
🌐 Example: Web server HTTPS (port 443)
SG Rule: Allow inbound TCP 443 from 0.0.0.0/0
Result:
✅ Request: Any IP → Instance:443 (allowed by rule)
✅ Response: Instance:443 → Any IP:ephemeral (auto allowed)
No need to configure outbound ephemeral ports! 🎉
2. Implicit Deny (Không có Explicit Deny)¶
❌ Security Group LIMITATION:
- Chỉ có ALLOW rules
- Không có EXPLICIT DENY
- Default = deny all (implicit)
⚠️ Problem: Không block được specific bad IPs
Example:
Rule: Allow 0.0.0.0/0 on port 443
Bad actor: 1.2.3.4 tấn công
SG không thể block riêng 1.2.3.4!
3. Attach to ENI (không phải instance)¶
🔧 Technical reality:
UI shows: "Attach SG to EC2 instance"
Actually: SG attaches to Primary ENI of instance
1 instance có thể nhiều ENI → nhiều SG
1 ENI có thể gán nhiều SG (max 5)
PHẦN 3: Advanced Features - Logical References¶
3.1. Reference Security Groups¶
🏗️ Architecture: Web tier → App tier
Web SG (A4L-web):
Inbound: Allow TCP 443 from 0.0.0.0/0
App SG (A4L-app):
Inbound: Allow TCP 1337 from A4L-web # ← Logical reference!
Meaning:
- Instances có A4L-web SG → có thể connect đến instances có A4L-app SG
- Không cần hardcode IP addresses
- Auto scale friendly!
3.2. Self-Referencing¶
🔄 Self-reference example:
App Cluster SG (A4L-cluster):
Inbound:
- Allow TCP 1337 from A4L-web # External access
- Allow ALL traffic from A4L-cluster # ← Self-reference!
Result:
- Instances trong cluster có thể nói chuyện với nhau
- Tự động adapt khi instances scale up/down
- Perfect cho: DB clusters, domain controllers, HA applications
PHẦN 4: Real-world Use Cases & Best Practices¶
4.1. Multi-tier Application¶
🏢 Typical 3-tier architecture:
Web SG:
- Inbound: HTTP/HTTPS from Internet
- Outbound: DB port to DB-SG
App SG:
- Inbound: App port from Web-SG
- Outbound: DB port to DB-SG
DB SG:
- Inbound: MySQL from Web-SG and App-SG
- Outbound: None needed (stateful response)
4.2. Auto Scaling Groups¶
⚡ Auto scaling scenario:
- New instances launch → auto get SG
- SG logical references still work
- No IP management overhead
- Zero manual intervention needed
4.3. Defense in Depth¶
🛡️ Security layers:
1. NACL: Block known bad IP ranges
2. Security Group: Allow legitimate traffic
3. OS firewall: Additional host-level protection
4. Application security: Input validation, etc.
PHẦN 5: Security Groups Limitations & Solutions¶
5.1. Cannot Block Specific IPs¶
❌ Problem:
SG rule: Allow 0.0.0.0/0 port 80
Bad actor: 192.168.1.100 attacking
Cannot block just 192.168.1.100!
✅ Solution:
NACL rule: DENY 192.168.1.100 (processed first)
SG rule: Allow 0.0.0.0/0 port 80
5.2. Default Behavior¶
🔒 New Security Group defaults:
Inbound: DENY ALL (empty rules)
Outbound: ALLOW ALL (to 0.0.0.0/0)
⚠️ Common mistake: Launch instance, forget inbound rules → cannot connect!
PHẦN 6: Checklist kiến thức thi cert & phỏng vấn¶
Core Concepts¶
- [x] SG là stateful, attach to ENI (not subnet/instance)
- [x] Chỉ có allow rules, không có explicit deny
- [x] Response traffic tự động allowed
- [x] Có thể reference other SGs và self-reference
- [x] Default: deny inbound, allow outbound
Advanced Features¶
- [x] Logical references giúp scale và simplify management
- [x] Self-reference cho intra-cluster communication
- [x] Multiple SGs per ENI (max 5)
- [x] Combine với NACL để có explicit deny capability
Câu hỏi exam thường gặp¶
- "SG attach vào đâu: instance, subnet hay ENI?"
- "Muốn block specific IP, dùng SG hay NACL?"
- "SG reference SG khác có nghĩa gì?"
- "Self-reference SG dùng để làm gì?"
- "EC2 launch mà không connect được, có thể do SG?"
PHẦN 7: So sánh cuối cùng - Khi nào dùng SG vs NACL?¶
Use Security Groups when:¶
- ✅ Primary access control cho instances
- ✅ Multi-tier applications
- ✅ Auto scaling environments
- ✅ Need logical resource references
- ✅ Simple rule management
Use NACL when:¶
- ✅ Need explicit deny (block bad actors)
- ✅ Compliance requirements
- ✅ Subnet-level broad filtering
- ✅ Defense in depth backup layer
Best Practice: Use both!¶
🎯 Recommended approach:
1. SG: Primary security, allow legitimate traffic
2. NACL: Secondary defense, block known threats
3. Both together = comprehensive security
PHẦN 8: Tổng kết¶
- Security Groups = instance-level intelligent firewall
- Stateful, logical references, auto-scaling friendly
- Perfect cho primary access control
- Limitation: Không block specific IPs (dùng NACL bổ sung)
- Core tool cho mọi AWS production deployment
Ghi nhớ: Security Groups là tool chính để secure EC2 instances. Hiểu rõ logical references và stateful behavior sẽ giúp bạn design security architecture hiệu quả và scale được!
Đọc lại bài này trước khi thiết kế VPC security hoặc chuẩn bị thi AWS Solutions Architect!