Ngày 4: VPC Fundamentals Review
Mục tiêu học tập
- Ôn tập các khái niệm cơ bản của VPC
- Hiểu rõ về Subnets, Route Tables, Gateways
- Nắm vững Security Groups và NACLs
1. VPC Overview
Định nghĩa
VPC (Virtual Private Cloud) là một mạng ảo riêng biệt trong AWS cloud, nơi bạn có toàn quyền kiểm soát:
- IP address range (CIDR block)
- Subnets
- Route tables
- Network gateways
- Security settings
VPC CIDR Block
Allowed range: /16 (65,536 IPs) to /28 (16 IPs)
Ví dụ:
10.0.0.0/16 → 10.0.0.0 - 10.0.255.255 (65,536 IPs)
172.16.0.0/16 → 172.16.0.0 - 172.16.255.255
192.168.0.0/16 → 192.168.0.0 - 192.168.255.255
Reserved IPs trong mỗi Subnet
Subnet: 10.0.1.0/24 (256 IPs total, 251 usable)
10.0.1.0 - Network address
10.0.1.1 - VPC Router
10.0.1.2 - DNS Server
10.0.1.3 - Reserved for future use
10.0.1.255 - Broadcast address (không dùng trong VPC)
2. Subnet Architecture
Public vs Private Subnet
Điều kiện để Subnet là Public
- Có route đến Internet Gateway
- Instances có Public IP hoặc Elastic IP
3. Route Tables
Cấu trúc Route Table
Destination Target Status Propagated
----------- ------ ------ ----------
10.0.0.0/16 local Active No
0.0.0.0/0 igw-xxxxx Active No
Route Priority
- Most specific route wins (longest prefix match)
- 10.0.1.0/24 ưu tiên hơn 10.0.0.0/16
Các loại Targets
| Target | Mô tả |
|---|---|
| local | Traffic trong VPC |
| igw-xxx | Internet Gateway |
| nat-xxx | NAT Gateway |
| vgw-xxx | Virtual Private Gateway |
| tgw-xxx | Transit Gateway |
| pcx-xxx | VPC Peering Connection |
| vpce-xxx | VPC Endpoint |
4. Internet Gateway (IGW)
Đặc điểm
- Horizontally scaled, redundant, highly available
- Một VPC chỉ attach được 1 IGW
- Perform NAT cho instances có public IP
Các bước để internet access
- Attach IGW vào VPC
- Thêm route 0.0.0.0/0 → IGW trong route table
- Associate route table với subnet
- Instances cần public IP
5. NAT Gateway vs NAT Instance
So sánh
| Feature | NAT Gateway | NAT Instance |
|---|---|---|
| Availability | Highly available trong AZ | Tự manage |
| Bandwidth | Up to 100 Gbps | Phụ thuộc instance type |
| Maintenance | Managed by AWS | Tự manage |
| Security Groups | Không hỗ trợ | Hỗ trợ |
| Bastion host | Không | Có thể |
| Cost | Theo giờ + data | Instance cost |
NAT Gateway Best Practice
6. Security Groups vs NACLs
So sánh chi tiết
| Feature | Security Group | NACL |
|---|---|---|
| Level | Instance (ENI) | Subnet |
| State | Stateful | Stateless |
| Rules | Allow only | Allow & Deny |
| Evaluation | All rules | Rules by number |
| Default | Deny all inbound | Allow all |
Stateful vs Stateless
Security Group (Stateful):
Request: Inbound port 80 ALLOWED → Response auto allowed
Response: Outbound auto allowed
NACL (Stateless):
Request: Inbound port 80 ALLOWED
Response: Outbound ephemeral ports MUST be explicitly allowed
Ephemeral Ports
Linux: 32768-65535
Windows: 49152-65535
NAT Gateway: 1024-65535
7. VPC Flow Logs
Các level capture
- VPC level: Tất cả ENIs trong VPC
- Subnet level: Tất cả ENIs trong subnet
- ENI level: Specific ENI
Destinations
- CloudWatch Logs
- S3 bucket
- Amazon Data Firehose (cũ: Kinesis Data Firehose)
Flow Log Record Format
<version> <account-id> <interface-id> <srcaddr> <dstaddr>
<srcport> <dstport> <protocol> <packets> <bytes>
<start> <end> <action> <log-status>
8. Câu hỏi ôn tập
-
Một VPC có thể có bao nhiêu Internet Gateway?
Xem đáp án
1 IGW per VPC — 1-to-1 relationship. IGW là highly available (horizontally scaled, redundant) — không phải SPOF. Không thể attach 2 IGWs vào 1 VPC, và 1 IGW không thể attach vào 2 VPCs cùng lúc. Để có internet access cho nhiều VPCs, mỗi VPC cần IGW riêng hoặc dùng centralized egress VPC qua Transit Gateway.
-
Sự khác biệt chính giữa Security Group và NACL là gì?
Xem đáp án
Security Group: instance-level (ENI), stateful (response auto-allowed), chỉ có Allow rules, evaluate all rules. NACL: subnet-level, stateless (cần explicit inbound + outbound rules), có cả Allow và Deny rules, rules evaluated theo số thứ tự. SG cho fine-grained instance control; NACL cho subnet-level block (ví dụ: block malicious IP với Deny rule — không thể làm với SG).
-
Tại sao cần NAT Gateway ở mỗi AZ?
Xem đáp án
NAT Gateway là AZ-scoped — chỉ phục vụ instances trong AZ đó. Nếu chỉ có 1 NAT GW ở AZ-a: (1) Traffic từ private instances ở AZ-b phải cross-AZ đến AZ-a → tốn cross-AZ data transfer cost ($0.01/GB), (2) Nếu AZ-a fail → tất cả private instances mất internet. Mỗi AZ có NAT GW riêng = HA + không có cross-AZ cost overhead.
-
Subnet nào được gọi là public subnet?
Xem đáp án
Subnet được gọi là public khi route table của nó có route
0.0.0.0/0 → IGW. Subnet tự nó không public hay private — routing table quyết định. Ngoài ra, instances trong public subnet cần Public IP hoặc Elastic IP để nhận inbound từ internet. Best practice: auto-assign public IP nên tắt, chỉ cấp Elastic IP cho instances thực sự cần. -
Flow Logs có thể gửi đến những destinations nào?
Xem đáp án
3 destinations: (1) CloudWatch Logs — real-time monitoring, Insights queries, alarms, (2) S3 bucket — long-term storage, Athena queries, rẻ hơn, (3) Amazon Data Firehose (tên cũ: Kinesis Data Firehose) — streaming đến Redshift, OpenSearch, S3. Có thể capture ở VPC level, Subnet level, hoặc ENI level. Một số traffic không capture: DHCP, IMDS, Amazon DNS (169.254.169.253).
9. Bài tập thực hành
- Tạo VPC với 2 public và 2 private subnets
- Configure NAT Gateway cho private subnets
- Tạo Security Group và test connectivity
- Enable VPC Flow Logs
Tài liệu tham khảo chính thức
- Amazon VPC User Guide
- VPC and Subnets
- Internet Gateways
- NAT Gateways
- Security Groups
- Network ACLs
- VPC Flow Logs
Ngày tiếp theo: EC2 và Auto Scaling Review