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

Tuần 4 - Ngày 4: VPC Endpoints và PrivateLink

Tuần 4 – Ngày 4

Tuần 4 - Ngày 4: VPC Endpoints và PrivateLink

Mục tiêu học tập

  • Phân biệt Gateway Endpoint và Interface Endpoint
  • Hiểu PrivateLink cho 3rd-party services
  • Nắm endpoint policy
  • Biết khi nào dùng VPC Endpoint vs NAT Gateway

1. Tổng quan VPC Endpoint

Vấn đề

Khi EC2 trong private subnet muốn access AWS services (S3, DynamoDB, SQS, KMS...):

  • Cách cũ: route 0.0.0.0/0 → NAT GW → IGW → AWS service public endpoint
  • Vấn đề: traffic đi qua internet → cost (NAT GW data processing), security concern

Giải pháp: VPC Endpoint

  • Private connection từ VPC đến AWS services
  • Traffic KHÔNG qua internet
  • Reduce NAT GW cost, improve security

3 loại endpoints

LoạiServicesCostSetup
Gateway EndpointS3, DynamoDBFreeRoute table entry
Interface Endpoint (PrivateLink)Hầu hết AWS services$0.01/hour + $0.01/GBENI trong subnet
Gateway Load Balancer EndpointCho appliance$/hourĐặc biệt cho firewall

2. Gateway Endpoint

Đặc điểm

  • Chỉ cho S3 và DynamoDB
  • Free (no extra charge)
  • Triển khai dạng route table entry (target = vpce-xxx)
  • Không tạo ENI, không tốn IP
  • Chỉ accessible từ VPC nơi endpoint tồn tại (không cross-VPC)

Setup

  1. Tạo Gateway Endpoint cho S3
  2. Chọn route table(s) liên quan
  3. AWS tự động add route: s3.us-east-1.amazonaws.com prefix list → vpce-xxx

Architecture

PrivateSubnet:EC2RouteTable10.0.0.0/16local0.0.0.0/0NATGWpl-63a5400a(S3)vpce-12345(GatewayEndpoint)S3Bucket(quaAWSnetwork)

Endpoint Policy

  • Default: allow all access đến service
  • Custom policy: restrict bucket, action, principal
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": "*",
    "Action": ["s3:GetObject", "s3:PutObject"],
    "Resource": "arn:aws:s3:::my-bucket/*"
  }]
}

Use case

  • EC2 trong private subnet upload/download S3 (giảm NAT GW cost)
  • Lambda VPC access DynamoDB
  • Compliance: traffic không leave VPC

Đặc điểm

  • Hầu hết AWS services support (100+): SQS, SNS, KMS, Secrets Manager, ECR, ECS, EventBridge, API Gateway, ...
  • Triển khai dạng ENI trong subnet với private IP
  • Truy cập qua DNS (private DNS có thể enable)
  • Cost: $0.01/hour per endpoint per AZ + $0.01/GB data processed
  • Support cross-VPC qua PrivateLink

Setup

  1. Tạo Interface Endpoint cho service (e.g., com.amazonaws.us-east-1.sqs)
  2. Chọn subnet(s) — tạo ENI trong mỗi subnet
  3. Chọn Security Group
  4. Enable Private DNS (recommended) → service URL public tự resolve sang private IP

DNS Resolution

Without Private DNS

SQS public URL: sqs.us-east-1.amazonaws.com
→ Resolves to public IP
→ Need to use endpoint-specific DNS: vpce-xxx.sqs.us-east-1.vpce.amazonaws.com
SQS public URL: sqs.us-east-1.amazonaws.com
→ Resolves to private IP của Interface Endpoint
→ Code không cần đổi

Endpoint Policy

  • Restrict actions, resources, principals
  • Apply per endpoint

Use case

  • Private subnet access SQS, Secrets Manager, KMS
  • Compliance (data không out internet)
  • Reduce NAT GW data processing cost

4. Endpoint Pricing Math Example

Scenario

EC2 trong private subnet pull image từ ECR, mỗi tháng 100 GB.

Option A: NAT Gateway (no endpoint)

  • NAT GW: $0.045/hour × 730 = $32.85/month
  • Data processing: $0.045/GB × 100 GB = $4.50/month
  • Total: ~$37.35/month

Option B: Interface Endpoint cho ECR

  • Endpoint: $0.01/hour × 730 × 1 AZ = $7.30/month
  • Data: $0.01/GB × 100 GB = $1.00/month
  • Total: ~$8.30/month (cần ECR Docker Registry endpoint cũng)

→ Tiết kiệm ~$29/month với endpoint cho 100 GB.

Quy tắc

  • Endpoint rẻ hơn NAT GW khi data lớn (> ~10 GB/month per service)
  • Multi-AZ Interface Endpoint = $0.01/hour × số AZ

Định nghĩa

PrivateLink = expose 1 service trong VPC của bạn để consumer ở VPC khác (cùng/khác account) access qua private network.

2 vai trò

  • Service provider: tạo VPC Endpoint Service từ NLB (or GWLB)
  • Service consumer: tạo Interface Endpoint connect đến service

Architecture

ProviderVPC(AccountA):EC2/ServiceNetworkLoadBalancerVPCEndpointService(servicename:com.amazonaws.vpce.xxx)PrivateLinkConsumerVPC(AccountB):InterfaceEndpoint(ENItrongsubnet)EC2(privatesubnet)

Use cases

  • SaaS provider expose API qua PrivateLink
  • Cross-account internal service sharing
  • Partner integration không qua internet

Allowlist

  • Service provider whitelist consumer accounts/principals
  • Consumer cần được approve mới connect được

6. Endpoint Services hỗ trợ Gateway/Interface

Gateway (free) — chỉ 2 services

  • S3
  • DynamoDB

Interface ($) — hầu hết khác

  • API Gateway (PrivateLink)
  • CloudWatch (Logs, Monitoring, Events)
  • SQS, SNS, EventBridge
  • KMS, Secrets Manager, Systems Manager
  • ECR, ECS, EKS
  • STS
  • Lambda (invoke endpoint)
  • DynamoDB cũng có Interface endpoint (mới — chọn 1 trong 2)
  • SageMaker, Bedrock
  • ...

Note: S3 Interface Endpoint

  • AWS đã add Interface Endpoint cho S3 (ngoài Gateway)
  • Khi nào dùng Interface cho S3?
    • Access S3 từ on-prem qua Direct Connect/VPN (Gateway không route được on-prem)
    • Cross-VPC qua Transit Gateway access S3 endpoint share
  • Default: dùng Gateway S3 endpoint (free)

7. Endpoint Policy Examples

Restrict S3 endpoint to specific bucket

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "AllowOnlyMyBucket",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:*",
    "Resource": [
      "arn:aws:s3:::my-bucket",
      "arn:aws:s3:::my-bucket/*"
    ]
  }]
}

Restrict S3 bucket to only my VPC Endpoint (Bucket Policy)

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "DenyExceptMyVPCE",
    "Effect": "Deny",
    "Principal": "*",
    "Action": "s3:*",
    "Resource": [
      "arn:aws:s3:::my-bucket",
      "arn:aws:s3:::my-bucket/*"
    ],
    "Condition": {
      "StringNotEquals": {
        "aws:SourceVpce": "vpce-1234567890abcdef"
      }
    }
  }]
}

→ Bucket chỉ accessible từ VPC này qua endpoint cụ thể.

8. Decision: VPC Endpoint vs NAT Gateway

SituationRecommendation
EC2 private subnet access chỉ S3 + DynamoDBGateway Endpoints (free), no NAT GW needed nếu không có internet access khác
EC2 access multiple AWS services + internetNAT GW + Interface Endpoints cho services có data lớn
Data transfer to S3 > 10 GB/monthGateway Endpoint chắc chắn
Need access 3rd-party SaaS via PrivateLinkPrivateLink Interface Endpoint
Strict compliance: no internetEndpoints cho mọi service + No NAT GW

9. Cost Optimization với Endpoints

Strategies

  1. S3 Gateway Endpoint (luôn dùng — free + giảm NAT cost)
  2. DynamoDB Gateway Endpoint
  3. Interface Endpoints cho services có > 10 GB/month data
  4. Consolidate AZs cho Interface Endpoints (mỗi AZ tăng $0.01/hour)
  5. Endpoint Policy restrict để tránh leak through

Common high-cost services qua NAT

  • ECR image pulls (big images)
  • CloudWatch Logs ingestion
  • S3 large object transfers
  • Secrets Manager / Parameter Store (frequent calls)

Câu hỏi ôn tập

  1. Gateway Endpoint hỗ trợ những services nào? Cost?

    Xem đáp án

    Gateway Endpoint chỉ hỗ trợ S3DynamoDB. Hoàn toàn miễn phí (không tốn tiền). Gateway Endpoint được add vào route table như một prefix list entry — traffic đến S3/DynamoDB route qua AWS network thay vì internet, không qua NAT Gateway (tiết kiệm NAT GW data processing fee $0.045/GB).

  2. Interface Endpoint có tạo ENI không? Tốn IP private không?

    Xem đáp án

    — Interface Endpoint tạo một ENI (Elastic Network Interface) trong subnet được chỉ định, với IP private từ subnet CIDR. Tốn 1 IP private per AZ per service. Interface Endpoint có phí: ~$0.01/giờ per AZ + $0.01/GB data processed. Hỗ trợ hầu hết AWS services (EC2, SSM, SNS, SQS, Kinesis, API Gateway...) và custom services qua PrivateLink.

  3. PrivateLink khác Interface Endpoint thế nào?

    Xem đáp án

    Interface Endpoint là consumer side (trong VPC của bạn) sử dụng PrivateLink để access AWS services hoặc 3rd-party services. AWS PrivateLink là technology/framework đằng sau — cho phép service providers (AWS hoặc bạn) expose services qua NLB, consumers access qua Interface Endpoint. PrivateLink = technology; Interface Endpoint = implementation consumer sử dụng.

  4. Để force S3 bucket chỉ accessible từ VPC Endpoint cụ thể, dùng condition gì?

    Xem đáp án

    Dùng condition aws:SourceVpce trong S3 Bucket Policy: "Condition": {"StringEquals": {"aws:SourceVpce": "vpce-xxxxxx"}}. Hoặc dùng aws:SourceVpc để allow toàn bộ VPC. Kết hợp với Deny rule: "Effect": "Deny", "Condition": {"StringNotEquals": {"aws:SourceVpce": "vpce-xxx"}} — chỉ allow access từ endpoint cụ thể, block tất cả access khác kể cả qua internet.

  5. Khi nào nên dùng VPC Endpoint thay vì NAT Gateway?

    Xem đáp án

    Khi traffic đến AWS services (S3, DynamoDB, SSM, SQS...) từ private subnet. Lý do: (1) Bảo mật — traffic không qua internet public, (2) Chi phí — Gateway Endpoint miễn phí, Interface Endpoint rẻ hơn NAT GW cho traffic volume lớn, (3) Latency — direct private network path. NAT Gateway vẫn cần cho internet access (non-AWS destinations). Pattern tốt nhất: VPC Endpoint cho AWS services + NAT GW cho internet thông thường.

Bài tập thực hành

  • Tạo Gateway Endpoint cho S3 trong VPC, observe route table thay đổi
  • Test EC2 trong private subnet (không NAT GW) access S3 qua endpoint
  • Tạo Interface Endpoint cho SQS với Private DNS enabled
  • Apply endpoint policy restrict S3 endpoint chỉ cho 1 bucket
  • Setup PrivateLink: tạo NLB + Endpoint Service trong 1 VPC, consume từ VPC khác

Tài liệu tham khảo chính thức


Tiếp theo: VPN và Direct Connect Intro