</>Học Dev
Bài học

Ngày 4: VPC Fundamentals Review

Tuần 1 – Ngày 4

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

VPCPublicSubnetPrivateSubnetWebServerDatabase(PublicIP)(PrivateIP)Route:0.0.0.0/0Route:0.0.0.0/0InternetGWNATGatewayInternetGWNATGateway

Điều kiện để Subnet là Public

  1. Có route đến Internet Gateway
  2. 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

TargetMô tả
localTraffic trong VPC
igw-xxxInternet Gateway
nat-xxxNAT Gateway
vgw-xxxVirtual Private Gateway
tgw-xxxTransit Gateway
pcx-xxxVPC Peering Connection
vpce-xxxVPC 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

  1. Attach IGW vào VPC
  2. Thêm route 0.0.0.0/0 → IGW trong route table
  3. Associate route table với subnet
  4. Instances cần public IP

5. NAT Gateway vs NAT Instance

So sánh

FeatureNAT GatewayNAT Instance
AvailabilityHighly available trong AZTự manage
BandwidthUp to 100 GbpsPhụ thuộc instance type
MaintenanceManaged by AWSTự manage
Security GroupsKhông hỗ trợHỗ trợ
Bastion hostKhôngCó thể
CostTheo giờ + dataInstance cost

NAT Gateway Best Practice

VPCAZ-1aAZ-1bNATGW1NATGW2PrivatePrivateSubnetSubnetMiAZcóNATGatewayriêngđtăngavailability

6. Security Groups vs NACLs

So sánh chi tiết

FeatureSecurity GroupNACL
LevelInstance (ENI)Subnet
StateStatefulStateless
RulesAllow onlyAllow & Deny
EvaluationAll rulesRules by number
DefaultDeny all inboundAllow 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

  1. VPC level: Tất cả ENIs trong VPC
  2. Subnet level: Tất cả ENIs trong subnet
  3. 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

  1. 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.

  2. 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).

  3. 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.

  4. 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.

  5. 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


Ngày tiếp theo: EC2 và Auto Scaling Review