Cho phép các ECS Fargate tasks trong private subnet có thể truy cập internet outbound (download packages từ NPM, gọi API AWS services, update container images) nhưng không có public IP trực tiếp.
Cost Optimization: Single-AZ NAT Gateway thay vì Multi-AZ để tiết kiệm chi phí (~$45/month thay vì $90/month).
📋 Prerequisites từ Task 4:
Trade-offs của Single-AZ:
🎯 Mục tiêu: Tạo Elastic IP để gán cho NAT Gateway, đảm bảo stable public IP address
# Load VPC resources từ Task 4
source vpc-resources.env
# Verify required resources exist
echo "VPC ID: $VPC_ID"
echo "Public Subnet 1A: $PUBLIC_SUBNET_1A"
echo "Private Route Tables: $PRIVATE_RT_1A, $PRIVATE_RT_1B"
⚠️ Kiểm tra điều kiện tiên quyết: Đảm bảo bạn đã hoàn thành Task 4 và có các resource IDs
AWS Console:
Truy cập EC2 Dashboard:
Cấp phát Elastic IP mới:
Thêm Tags:
Hoàn thành cấp phát:

CLI (nếu cần):
# Allocate Elastic IP trong VPC domain
aws ec2 allocate-address --domain vpc \
--tag-specifications 'ResourceType=elastic-ip,Tags=[{Key=Name,Value=vinashoes-nat-eip},{Key=Purpose,Value=NAT-Gateway}]'
# Save Allocation ID
export EIP_ALLOC_ID=eipalloc-0abc123def456789a
💡 Các phương pháp tốt nhất cho Elastic IP:
Console Verification:
CLI Verification:
# Verify EIP details
aws ec2 describe-addresses --allocation-ids $EIP_ALLOC_ID --output table
🎯 Mục tiêu: Triển khai NAT Gateway trong public subnet AZ-1a để cung cấp internet access cho private subnets
AWS Console:
Truy cập VPC Dashboard:
Tạo NAT Gateway:
Cấu hình NAT Gateway:
Thêm Tags:
Xem lại và Tạo:

Chờ trạng thái Available:
pending → availableAvailable khi ready
CLI (nếu cần):
# Create NAT Gateway
aws ec2 create-nat-gateway --subnet-id $PUBLIC_SUBNET_1A --allocation-id $EIP_ALLOC_ID \
--tag-specifications 'ResourceType=nat-gateway,Tags=[{Key=Name,Value=vinashoes-nat-gw}]'
# Save NAT Gateway ID
export NAT_GW_ID=nat-0def456ghi789012b
# Wait for available state
aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT_GW_ID
Console Verification:
CLI Verification:
# Get detailed NAT Gateway info
aws ec2 describe-nat-gateways --nat-gateway-ids $NAT_GW_ID --output table
⚠️ Important Notes:
💰 Cost Optimization:
🎯 Mục tiêu: Route traffic từ cả 2 private subnets qua NAT Gateway để enable internet access
AWS Console:
Truy cập VPC Route Tables:
Chỉnh sửa Private Route Table AZ-1a:
vinashoes-private-rt-1aThêm Route NAT Gateway:

AWS Console:
Chỉnh sửa Private Route Table AZ-1b:
vinashoes-private-rt-1bThêm Route NAT Gateway:

Expected Route Tables:
Private RT AZ-1a:
Destination Target State
10.0.0.0/16 local active
0.0.0.0/0 nat-0def456ghi789012b active
Private RT AZ-1b:
Destination Target State
10.0.0.0/16 local active
0.0.0.0/0 nat-0def456ghi789012b active
CLI (nếu cần):
# Add routes cho both private route tables
aws ec2 create-route --route-table-id $PRIVATE_RT_1A --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_GW_ID
aws ec2 create-route --route-table-id $PRIVATE_RT_1B --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_GW_ID
# Verify routes
aws ec2 describe-route-tables --route-table-ids $PRIVATE_RT_1A $PRIVATE_RT_1B --output table
Cross-AZ Routing Explanation:
✅ Route Tables Configuration Checklist:
🎯 Mục tiêu: Xác minh private subnets có thể access internet thông qua NAT Gateway
Xác minh cơ sở hạ tầng:
AWS Console:
Khởi chạy EC2 Instance:
Cấu hình Security Group (nếu cần):

Phương pháp 1: Systems Manager Session Manager
AWS Console:
Truy cập Systems Manager:
Bắt đầu Session:
Test Commands:
# Test DNS resolution
nslookup google.com
# Test HTTP connectivity
curl -I https://httpbin.org/ip
# Check external IP (should be Elastic IP)
curl https://checkip.amazonaws.com
# Test package updates
sudo yum update -y

Kết quả mong đợi:
Test AZ-1b Connectivity:

CLI (nếu cần):
# Create test instance
aws ec2 run-instances \
--image-id ami-0c02fb55956c7d316 \
--instance-type t3.micro \
--subnet-id $PRIVATE_SUBNET_1A \
--security-group-ids $PRIVATE_SG_ID \
--no-associate-public-ip-address
# Test connectivity via Session Manager
aws ssm start-session --target $INSTANCE_ID
1. Connection Timeout:
2. DNS Resolution Fails:
3. High Data Transfer Costs:
✅ Danh sách kiểm tra Testing:
🗒️ Clean Up Prerequisites:
aws CLI configured with appropriate permissionssource vpc-resources.envRemove routes from Private Route Tables:
# Load environment variables
source vpc-resources.env
# Remove NAT Gateway route from Private RT AZ-1a
aws ec2 delete-route --route-table-id $PRIVATE_RT_1A --destination-cidr-block 0.0.0.0/0
# Remove NAT Gateway route from Private RT AZ-1b
aws ec2 delete-route --route-table-id $PRIVATE_RT_1B --destination-cidr-block 0.0.0.0/0
echo "✅ NAT Gateway routes removed from private route tables"
Delete the NAT Gateway:
# Delete NAT Gateway
aws ec2 delete-nat-gateway --nat-gateway-id $NAT_GW_ID
# Wait for deletion to complete (takes 1-2 minutes)
echo "Waiting for NAT Gateway deletion..."
aws ec2 wait nat-gateway-deleted --nat-gateway-ids $NAT_GW_ID
echo "✅ NAT Gateway deleted successfully"
Release the allocated Elastic IP:
# Release Elastic IP
aws ec2 release-address --allocation-id $EIP_ALLOC_ID
echo "✅ Elastic IP released successfully"
Verify all resources are deleted:
# Check NAT Gateway status (should not exist)
aws ec2 describe-nat-gateways --nat-gateway-ids $NAT_GW_ID 2>/dev/null || echo "NAT Gateway deleted"
# Check Elastic IP status (should not exist)
aws ec2 describe-addresses --allocation-ids $EIP_ALLOC_ID 2>/dev/null || echo "Elastic IP released"
# Verify route tables no longer have NAT Gateway routes
aws ec2 describe-route-tables --route-table-ids $PRIVATE_RT_1A $PRIVATE_RT_1B \
--query 'RouteTables[].Routes[?GatewayId==`nat-*`]'
echo "✅ Clean up verification complete"
⚠️ Important Notes:
💰 Cost Breakdown: Understanding NAT Gateway costs cho informed decision making
| Component | Formula | Cost (USD) | Notes |
|---|---|---|---|
| NAT Gateway Hourly | 730 hours × $0.045/hour | $32.85 | Fixed cost regardless of usage |
| Data Processing | 1TB × $0.045/GB | $45.00 | Per GB processed by NAT Gateway |
| Cross-AZ Transfer | 500GB × $0.01/GB | $5.00 | AZ-1b → AZ-1a traffic |
| Total (1TB scenario) | $82.85 | For moderate usage |
| Architecture | Monthly Cost | Pros | Cons |
|---|---|---|---|
| Single NAT Gateway | ~$83 (1TB) | Cost effective, simple | Single point failure |
| Multi-AZ NAT Gateway | ~$166 (1TB) | High availability | 2x NAT Gateway costs |
| NAT Instance (t3.micro) | ~$9 + data | Very cheap | Manual maintenance, limited bandwidth |
| Khối lượng Data Transfer | Single NAT Gateway | Multi-AZ NAT Gateway | NAT Instance | Khuyến nghị |
|---|---|---|---|---|
| 100GB/tháng | $41.85 | $83.70 | $14.50 | NAT Instance (tiết kiệm nhất) |
| 500GB/tháng | $55.35 | $110.70 | $29.50 | NAT Instance |
| 1TB/tháng | $82.85 | $165.70 | $54.50 | Single NAT Gateway |
| 2TB/tháng | $137.85 | $275.70 | $104.50 | Single NAT Gateway |
| 5TB/tháng | $270.35 | $540.70 | $249.50 | Single NAT Gateway |
💡 Mẹo tối ưu hóa chi phí:
CLI (nếu cần):
# Monitor NAT Gateway costs
aws cloudwatch get-metric-statistics \
--namespace AWS/NatGateway \
--metric-name BytesOutToDestination \
--dimensions Name=NatGatewayId,Value=$NAT_GW_ID \
--start-time $(date -u -d '24 hours ago' +%Y-%m-%dT%H:%M:%S) \
--end-time $(date -u +%Y-%m-%dT%H:%M:%S) \
--period 3600 \
--statistics Sum
1. No Internet Access:
2. High Data Transfer Costs:
3. Performance Issues:
4. DNS Resolution Fails:
nslookup google.com1. Security:
2. Performance:
3. Cost Management:
4. Monitoring:
| Resource | Status | Configuration | Purpose |
|---|---|---|---|
| Elastic IP | ✅ Created | Static public IP | Stable outbound IP address |
| NAT Gateway | ✅ Deployed | AZ-1a public subnet | Internet access cho private subnets |
| Route Tables | ✅ Configured | 0.0.0.0/0 → NAT Gateway | Route private traffic to internet |
| Testing | ✅ Verified | Both AZ connectivity | Cross-AZ routing working |
🎉 Task 5 Complete!
Private subnets có secure internet access qua NAT Gateway. ECS Fargate tasks có thể pull container images và communicate with external APIs.
Infrastructure Cost: ~$83/month (1TB traffic scenario)
Next Step: Task 6 - VPC Endpoints để optimize costs (~30-50% reduction)
Các tối ưu hóa sắp tới:
Task 6 sẽ significantly optimize your infrastructure costs while maintaining performance.