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ại | Services | Cost | Setup |
|---|---|---|---|
| Gateway Endpoint | S3, DynamoDB | Free | Route table entry |
| Interface Endpoint (PrivateLink) | Hầu hết AWS services | $0.01/hour + $0.01/GB | ENI trong subnet |
| Gateway Load Balancer Endpoint | Cho 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
- Tạo Gateway Endpoint cho S3
- Chọn route table(s) liên quan
- AWS tự động add route:
s3.us-east-1.amazonaws.com prefix list → vpce-xxx
Architecture
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
3. Interface Endpoint (PrivateLink)
Đặ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
- Tạo Interface Endpoint cho service (e.g.,
com.amazonaws.us-east-1.sqs) - Chọn subnet(s) — tạo ENI trong mỗi subnet
- Chọn Security Group
- 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
With Private DNS (recommended)
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
5. PrivateLink cho 3rd-Party Services
Đị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
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
| Situation | Recommendation |
|---|---|
| EC2 private subnet access chỉ S3 + DynamoDB | Gateway Endpoints (free), no NAT GW needed nếu không có internet access khác |
| EC2 access multiple AWS services + internet | NAT GW + Interface Endpoints cho services có data lớn |
| Data transfer to S3 > 10 GB/month | Gateway Endpoint chắc chắn |
| Need access 3rd-party SaaS via PrivateLink | PrivateLink Interface Endpoint |
| Strict compliance: no internet | Endpoints cho mọi service + No NAT GW |
9. Cost Optimization với Endpoints
Strategies
- S3 Gateway Endpoint (luôn dùng — free + giảm NAT cost)
- DynamoDB Gateway Endpoint
- Interface Endpoints cho services có > 10 GB/month data
- Consolidate AZs cho Interface Endpoints (mỗi AZ tăng $0.01/hour)
- 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
-
Gateway Endpoint hỗ trợ những services nào? Cost?
Xem đáp án
Gateway Endpoint chỉ hỗ trợ S3 và DynamoDB. 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).
-
Interface Endpoint có tạo ENI không? Tốn IP private không?
Xem đáp án
Có — 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.
-
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.
-
Để force S3 bucket chỉ accessible từ VPC Endpoint cụ thể, dùng condition gì?
Xem đáp án
Dùng condition
aws:SourceVpcetrong S3 Bucket Policy:"Condition": {"StringEquals": {"aws:SourceVpce": "vpce-xxxxxx"}}. Hoặc dùngaws: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. -
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