14.NAT2
🌐 NAT Gateway Advanced: Multi-AZ Design, NAT Instance vs Gateway, IPv6 (Phần 2 - Đủ thi cert & đi làm)¶
PHẦN 1: Multi-AZ NAT Gateway Design (CRITICAL cho thi)¶
Myth vs Reality về NAT Gateway Resilience¶
❌ MYTH: "1 NAT Gateway đủ cho cả region"
✅ REALITY: NAT Gateway chỉ AZ-resilient, KHÔNG regional-resilient
Example architecture:
us-east-1a: NAT-GW-A + Private Route Table A → NAT-GW-A
us-east-1b: NAT-GW-B + Private Route Table B → NAT-GW-B
us-east-1c: NAT-GW-C + Private Route Table C → NAT-GW-C
Rule: 1 AZ fail → chỉ private subnets trong AZ đó mất Internet
Resilient NAT Architecture¶
🏗️ Production-ready design:
Public Subnets (Web Tier):
- us-east-1a: NAT Gateway A (+ Elastic IP)
- us-east-1b: NAT Gateway B (+ Elastic IP)
- us-east-1c: NAT Gateway C (+ Elastic IP)
Private Subnets (App Tier):
- us-east-1a: Route Table A → NAT Gateway A (same AZ)
- us-east-1b: Route Table B → NAT Gateway B (same AZ)
- us-east-1c: Route Table C → NAT Gateway C (same AZ)
Critical: Mỗi private subnet route đến NAT Gateway cùng AZ!
PHẦN 2: NAT Instance vs NAT Gateway (So sánh chi tiết)¶
NAT Instance (EC2 based) - Legacy approach¶
⚙️ NAT Instance characteristics:
Pros:
✅ Cheaper (especially small/test environments)
✅ Free tier eligible possible
✅ Can multipurpose (bastion host, port forwarding)
✅ Supports Security Groups
✅ Full control over instance
Cons:
❌ Single point of failure
❌ Manual maintenance (OS updates, patches)
❌ Limited by instance type performance
❌ Need to disable source/destination checks
❌ Manual scaling
Source/Destination Check (Cực quan trọng cho NAT Instance)¶
🔧 Critical setting for NAT Instance:
Problem:
EC2 mặc định drop traffic nếu nó không phải source/destination
Solution:
EC2 Console → Right click instance → Change Source/Dest Check → Disable
Why needed:
NAT traffic: Private_Instance → NAT_Instance → Internet
NAT Instance nhận traffic có source = Private_Instance (not itself)
Must disable check để allow forwarding!
Decision Matrix: Khi nào dùng gì?¶
🎯 Choose NAT Gateway when:
✅ Production environments
✅ High availability requirements
✅ High bandwidth needs (up to 45 Gbps)
✅ Minimal maintenance overhead
✅ AWS recommended approach
🎯 Choose NAT Instance when:
✅ Cost is primary concern
✅ Need bastion host functionality
✅ Need port forwarding
✅ Need custom filtering (Security Groups)
✅ Test/development environments
PHẦN 3: IPv6 và NAT (Cực hay ra đề thi!)¶
IPv6 Fundamental Difference¶
💡 Key concept: NAT KHÔNG cần thiết cho IPv6!
IPv4:
- Private addresses (RFC 1918) cần NAT để ra Internet
- IGW làm Static NAT (1:1)
- NAT Gateway làm IP masquerading (many:1)
IPv6:
- TẤT CẢ IPv6 addresses trong AWS đều publicly routable
- Không có concept "private IPv6"
- IGW pass-through trực tiếp, không NAT
IPv6 Internet Access¶
🌐 IPv6 instance access Internet:
Method 1 - Bidirectional (Public-like):
Route Table: ::/0 → Internet Gateway
Result: Instance có thể nhận inbound connections
Method 2 - Outbound-only (cho private):
Route Table: ::/0 → Egress-only Internet Gateway
Result: Chỉ outbound, không nhận inbound
NAT Gateway và IPv6¶
❌ CRITICAL: NAT Gateway KHÔNG support IPv6!
Exam trap questions:
"How to give private IPv6 instance Internet access?"
Wrong: "Use NAT Gateway" ← This is WRONG!
Right: "Use Egress-only Internet Gateway"
Remember: NAT Gateway = IPv4 only!
PHẦN 4: Advanced NAT Considerations¶
Cost Optimization Strategies¶
💰 NAT Gateway cost optimization:
1. Consolidate traffic:
Multiple private subnets → 1 NAT Gateway (same AZ)
2. VPC Endpoints for AWS services:
S3, DynamoDB → VPC Endpoint (free)
Instead of: Instance → NAT → Internet → S3
3. Right-size by monitoring:
CloudWatch metrics → optimize data transfer
4. Consider NAT Instance for dev/test:
Lower cost, acceptable for non-production
Security Considerations¶
🔒 NAT Gateway security:
Cannot do:
❌ Assign Security Groups to NAT Gateway
❌ SSH into NAT Gateway (managed service)
❌ Port forwarding through NAT Gateway
❌ Use as bastion host
Can do:
✅ Use NACLs on public subnet (where NAT Gateway sits)
✅ Monitor via VPC Flow Logs
✅ CloudTrail for API calls
✅ CloudWatch for metrics
Troubleshooting Common Issues¶
🐛 "Private instance can't reach Internet via NAT"
Checklist:
1. ✅ NAT Gateway in public subnet?
2. ✅ Public subnet has 0.0.0.0/0 → IGW route?
3. ✅ NAT Gateway has Elastic IP attached?
4. ✅ Private subnet has 0.0.0.0/0 → NAT Gateway route?
5. ✅ Private subnet and NAT Gateway in same AZ?
6. ✅ Security Groups allow outbound traffic?
7. ✅ NACLs not blocking traffic?
PHẦN 5: Checklist kiến thức thi cert & phỏng vấn¶
Core Facts¶
- [x] NAT Gateway chỉ AZ-resilient, cần multi-AZ design
- [x] NAT Gateway KHÔNG support IPv6
- [x] NAT Instance cần disable source/destination checks
- [x] NAT Gateway không support Security Groups
- [x] IPv6 dùng Egress-only IGW cho outbound-only
Architecture Design¶
- [x] 1 NAT Gateway per AZ cho high availability
- [x] Private subnets route to NAT Gateway trong cùng AZ
- [x] NAT Gateway phải có Elastic IP
- [x] Public subnet chứa NAT Gateway cần IGW route
Câu hỏi exam killer¶
- "NAT Gateway có regional resilient không?" ❌ Không, chỉ AZ-resilient
- "IPv6 instance cần NAT Gateway để access Internet?" ❌ Không, dùng IGW hoặc Egress-only IGW
- "Có thể assign Security Group cho NAT Gateway không?" ❌ Không thể
- "NAT Instance vs NAT Gateway, chọn gì cho production?" NAT Gateway
- "Muốn IPv6 outbound-only, dùng gì?" Egress-only Internet Gateway
PHẦN 6: Production Best Practices¶
High Availability Design¶
🏗️ Enterprise-grade NAT architecture:
Multi-AZ deployment:
✅ 1 NAT Gateway per AZ
✅ Cross-AZ failover via routing
✅ Monitor with CloudWatch
✅ Automate with CloudFormation/Terraform
Cost vs Availability trade-off:
- Single NAT: Cheaper, single point of failure
- Multi-AZ NAT: More expensive, highly available
Monitoring & Alerting¶
📊 Key metrics to monitor:
CloudWatch Metrics:
- BytesInFromDestination/Source
- PacketsInFromDestination/Source
- ActiveConnectionCount
- ConnectionAttemptCount
- ConnectionEstablishedCount
Alarms:
- High bandwidth usage
- Connection failures
- NAT Gateway health
PHẦN 7: Tổng kết¶
- NAT Gateway = managed, AZ-resilient (not regional)
- Multi-AZ design critical cho production HA
- IPv6 KHÔNG cần NAT Gateway
- NAT Instance cheaper nhưng more maintenance
- Security Groups KHÔNG work với NAT Gateway
- Cost optimization với VPC Endpoints cho AWS services
Tiếp theo: Demo implement resilient multi-AZ NAT Gateway architecture trong Animals4Life VPC!
Ghi nhớ: Hiểu rõ multi-AZ requirements và IPv6 behavior sẽ giúp bạn ace các câu hỏi khó nhất về NAT trong AWS exams!