Bỏ qua đến nội dung
DevOps Lab

AWS VPC Connectivity

Hiểu sâu PrivateLink, VPC Peering và Transit Gateway — kiến trúc, trade-offs, và interactive simulation.

☁️ AWS Networking Deep Dive

AWS VPC
Connectivity

Hiểu sâu PrivateLink, VPC Peering và Transit Gateway — kiến trúc, trade-offs, và interactive simulation để thấy tận mắt traffic đi như thế nào.

3
Connectivity models
5
Architecture diagrams
3
Live simulations
10+
Config examples
1
Introduction
AWS VPC fundamentals và bài toán connectivity giữa các VPC

Amazon VPC (Virtual Private Cloud) là mạng ảo riêng biệt trong AWS cloud — bạn có toàn quyền kiểm soát IP range, subnet, routing table, security group, và network ACL. Vấn đề phức tạp bắt đầu khi nhiều VPC cần giao tiếp với nhau hoặc với dịch vụ AWS một cách bảo mật.

🔗
VPC Peering
Kết nối point-to-point trực tiếp giữa 2 VPC. Traffic đi qua AWS backbone, không qua internet. Đơn giản nhưng không scale tốt khi có nhiều VPC.
🔒
AWS PrivateLink
Expose dịch vụ qua Interface Endpoint (ENI). Consumer VPC chỉ thấy endpoint IP — không biết gì về provider network. Bảo mật tuyệt đối, unidirectional.
🌐
Transit Gateway
Hub-and-spoke model — một TGW kết nối hàng trăm VPC, on-premises, và VPN. Centralized routing, đơn giản hóa topology khi scale lớn.

Vấn Đề Cần Giải Quyết

🏗 Bài Toán: Kết Nối Nhiều VPC Trong AWS
❌ Naive: qua Internet = insecure + latency VPC A App Server Database Internet ⚠ Exposed VPC B Service VS ✓ AWS native = private + fast + secure VPC A Consumer Endpoint Private Sub AWS Backbone VPC B Provider NLB Data đi qua internet — rủi ro bảo mật Traffic stays on AWS private network
2
Architecture Comparison
Traditional vs PrivateLink, VPC Peering, Transit Gateway — trade-offs thực tế

Traditional vs PrivateLink Architecture

❌ Traditional (qua Internet / VPN)
⚠️
Data rời AWS network, đi qua public internet — exposed to MITM, DDoS
🐢
Latency cao do routing phức tạp qua nhiều hop
💸
Chi phí egress data cao ($0.09/GB ra internet)
🔧
Cần manage IGW, NAT Gateway, VPN endpoint, certificates
🌐
IP range của 2 VPC có thể conflict nếu không plan từ đầu
✓ AWS PrivateLink
🔒
Traffic hoàn toàn trong AWS network — không bao giờ ra internet
Low latency, high throughput nhờ AWS backbone infrastructure
💰
Chi phí thấp hơn: $0.01/hr per endpoint + $0.01/GB
🛡️
Provider VPC hoàn toàn ẩn — consumer chỉ biết endpoint IP
CIDR overlap OK — endpoint dùng IP riêng, không conflict
🔒 AWS PrivateLink — Architecture Flow
Consumer VPC (10.0.0.0/16) App Server 10.0.1.10 Private Subnet 10.0.2.0/24 Interface Endpoint ENI 10.0.3.5 (ENI IP) AWS PrivateLink Private backbone network ✓ Never touches internet Provider VPC (172.16.0.0/16) NLB Network LB Endpoint Service Backend 1 :8080 Backend 2 :8080 IP: 10.0.3.5 (ENI) — CIDR overlap OK ✓ Provider topology: hoàn toàn ẩn ✓

VPC Peering — Point-to-Point

VPC Peering tạo một kết nối network trực tiếp giữa 2 VPC. Traffic đi qua AWS backbone — không qua internet, không qua gateway. Đơn giản nhưng có giới hạn quan trọng: không transitive.

🔗 VPC Peering — Non-Transitive Problem
Peering works (direct) VPC A 10.0.0.0/16 VPC B 10.1.0.0/16 VPC C 10.2.0.0/16 pcx-1 pcx-2 pcx-3 A↔B ✓, A↔C ✓, B↔C ✓ Cần N×(N-1)/2 peering connections ❌ Non-transitive: A→B→D KHÔNG work VPC A 10.0.0.0/16 VPC B 10.1.0.0/16 VPC D 10.3.0.0/16 🚫 BLOCKED Transitive routing not allowed 10 VPCs = 45 peering connections! → Transit Gateway giải quyết vấn đề này

Transit Gateway — Hub & Spoke

Transit Gateway là một regional virtual router — tất cả VPC, VPN, Direct Connect đều attach vào TGW. Thay vì N×(N-1)/2 peering, chỉ cần N attachments. Centralized routing table, policy-based forwarding.

🌐 Transit Gateway — Hub & Spoke Topology
Transit Gateway Route Tables VPC Prod 10.0.0.0/16 VPC Dev 10.1.0.0/16 VPC Shared Services VPC Data Analytics On-Premises VPN / DX TGW Peering Other Region attachment attachment attachment attachment 5 VPCs/Networks 5 attachments (không phải 10!) vs 10 peering connections
Interactive Simulation
Click để gửi packet và xem traffic flow qua từng model — real-time logs
🎮
Cách dùng simulation
Chọn tab (PrivateLink / VPC Peering / Transit GW), chọn source & destination, nhấn Send Packet để xem animation và log chi tiết. Thử gửi packet đến endpoint không cho phép để thấy packet bị block.

Config Examples

PrivateLink — VPC Endpoint Service (Provider side)

TERRAFORMprivatelink-provider.tf
# ── Provider VPC: Tạo NLB + Endpoint Service ──────────────
resource "aws_lb" "provider_nlb" {
  name               = "provider-nlb"
  load_balancer_type = "network"
  internal           = true   # PHẢI là internal
  subnets            = [aws_subnet.provider_private.id]
}

resource "aws_vpc_endpoint_service" "my_service" {
  acceptance_required        = true   # Manual approve consumers
  network_load_balancer_arns = [aws_lb.provider_nlb.arn]

  # Whitelist AWS accounts được phép connect
  allowed_principals = [
    "arn:aws:iam::123456789012:root",  # Account A
    "arn:aws:iam::987654321098:root",  # Account B
  ]

  tags = { Name = "my-private-service" }
}

# Output: service_name để consumer dùng
output "endpoint_service_name" {
  value = aws_vpc_endpoint_service.my_service.service_name
  # → "com.amazonaws.vpce.ap-southeast-1.vpce-svc-xxx"
}

PrivateLink — Interface Endpoint (Consumer side)

TERRAFORMprivatelink-consumer.tf
# ── Consumer VPC: Tạo Interface Endpoint ──────────────────
resource "aws_vpc_endpoint" "to_provider" {
  vpc_id            = aws_vpc.consumer.id
  service_name      = var.provider_service_name
  vpc_endpoint_type = "Interface"

  # ENI sẽ được tạo trong các subnet này
  subnet_ids        = [aws_subnet.consumer_private.id]
  security_group_ids = [aws_security_group.endpoint_sg.id]

  # Private DNS: resolve service hostname → ENI IP
  private_dns_enabled = true

  tags = { Name = "endpoint-to-provider" }
}

# Security Group cho endpoint — chỉ cho phép từ app servers
resource "aws_security_group" "endpoint_sg" {
  vpc_id = aws_vpc.consumer.id
  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.0/8"]  # Only internal
  }
}

# Output: DNS name để app dùng (thay vì IP trực tiếp)
output "endpoint_dns" {
  value = aws_vpc_endpoint.to_provider.dns_entry[0].dns_name
}

Transit Gateway — Full Setup

TERRAFORMtransit-gateway.tf
# ── Tạo Transit Gateway ───────────────────────────────────
resource "aws_ec2_transit_gateway" "main" {
  description                     = "Central TGW for all VPCs"
  default_route_table_association = "enable"
  default_route_table_propagation = "enable"
  auto_accept_shared_attachments  = "disable"  # Manual approve
  dns_support                     = "enable"
  tags = { Name = "main-tgw" }
}

# ── Attach VPCs vào TGW ──────────────────────────────────
resource "aws_ec2_transit_gateway_vpc_attachment" "prod" {
  subnet_ids         = [aws_subnet.prod_tgw_subnet.id]
  transit_gateway_id = aws_ec2_transit_gateway.main.id
  vpc_id             = aws_vpc.prod.id
  tags = { Name = "prod-attachment" }
}

# ── Custom Route Table (segment traffic) ─────────────────
# Prod VPC không được route đến Dev VPC
resource "aws_ec2_transit_gateway_route_table" "prod_rt" {
  transit_gateway_id = aws_ec2_transit_gateway.main.id
  tags = { Name = "prod-route-table" }
}

resource "aws_ec2_transit_gateway_route" "to_shared" {
  destination_cidr_block         = "10.100.0.0/16"  # Shared Services VPC
  transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.prod_rt.id
  transit_gateway_attachment_id  = aws_ec2_transit_gateway_vpc_attachment.shared.id
}

# ── VPC Route Table: route đến TGW ───────────────────────
resource "aws_route" "vpc_to_tgw" {
  route_table_id         = aws_route_table.prod_private.id
  destination_cidr_block = "10.0.0.0/8"    # All internal traffic
  transit_gateway_id     = aws_ec2_transit_gateway.main.id
}
4
Khi Nào Dùng Gì?
Decision matrix để chọn đúng connectivity model
Tiêu chí VPC Peering PrivateLink Transit Gateway
Số VPCs2–5 VPCsKhông giới hạn5–5000 VPCs
Traffic directionBidirectionalUnidirectionalBidirectional
CIDR overlapKhông hỗ trợ ❌Hỗ trợ ✓Không hỗ trợ ❌
Transitive routingKhông ❌N/ACó ✓
On-premisesKhông ❌N/AVPN/DX ✓
BandwidthUnlimited10 Gbps per AZ50 Gbps burst
Chi phí$0.01/GB$0.01/hr + $0.01/GB$0.05/hr + $0.02/GB
Setup phức tạpThấpTrung bìnhCao
Security modelSG + NACLProvider fully hiddenRoute table segmentation
Multi-accountHỗ trợHỗ trợ tốtRAM sharing
Chọn VPC Peering khi...
  • Chỉ có 2–4 VPCs cần kết nối
  • Cần bidirectional traffic đầy đủ
  • Không có CIDR conflict
  • Muốn setup đơn giản, chi phí thấp nhất
  • Ví dụ: App VPC ↔ Database VPC
Chọn PrivateLink khi...
  • Expose 1 service cho nhiều consumers
  • Provider muốn ẩn hoàn toàn network topology
  • CIDR conflict giữa các VPC
  • SaaS product cần expose cho khách hàng
  • Ví dụ: Payment service, Auth service
Chọn Transit Gateway khi...
  • Có 5+ VPCs cần kết nối lẫn nhau
  • Cần kết nối on-premises qua VPN/DX
  • Muốn centralized network policy
  • Multi-region, multi-account enterprise
  • Ví dụ: Landing Zone, Hub-spoke enterprise
AWS VPC Deep Dive · Network Connectivity Series