AWS VPC Connectivity
Hiểu sâu PrivateLink, VPC Peering và Transit Gateway — kiến trúc, trade-offs, và interactive simulation.
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.
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.
Vấn Đề Cần Giải Quyết
Traditional vs PrivateLink Architecture
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.
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.
Config Examples
PrivateLink — VPC Endpoint Service (Provider side)
# ── 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)
# ── 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
# ── 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 }
| Tiêu chí | VPC Peering | PrivateLink | Transit Gateway |
|---|---|---|---|
| Số VPCs | 2–5 VPCs | Không giới hạn | 5–5000 VPCs |
| Traffic direction | Bidirectional | Unidirectional | Bidirectional |
| CIDR overlap | Không hỗ trợ ❌ | Hỗ trợ ✓ | Không hỗ trợ ❌ |
| Transitive routing | Không ❌ | N/A | Có ✓ |
| On-premises | Không ❌ | N/A | VPN/DX ✓ |
| Bandwidth | Unlimited | 10 Gbps per AZ | 50 Gbps burst |
| Chi phí | $0.01/GB | $0.01/hr + $0.01/GB | $0.05/hr + $0.02/GB |
| Setup phức tạp | Thấp | Trung bình | Cao |
| Security model | SG + NACL | Provider fully hidden | Route table segmentation |
| Multi-account | Hỗ trợ | Hỗ trợ tốt | RAM sharing |
- 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
- 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
- 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