Amazon CloudFront
CDN with 750+ edge PoPs + regional edge caches. Exam themes: origin choices (S3 with OAC, VPC origin, custom), signed URLs vs signed cookies, edge compute (Functions vs Lambda@Edge), and how to lock origins so users can only enter via CloudFront.
CDN với 750+ edge PoP + regional edge cache. Chủ đề exam: origin (S3 + OAC, VPC origin, custom), signed URL vs signed cookie, edge compute (Functions vs Lambda@Edge), và cách chặn origin để user chỉ vào qua CloudFront.
✓ Reach for it✓ Nên chọn
- Static assets + SPA in front of S3Asset tĩnh + SPA trước S3
- DDoS / WAF layer for a global siteLớp DDoS / WAF cho site toàn cầu
- Video / large file distributionPhân phối video / file lớn
- Cheap "many regions" for reads"Nhiều region" rẻ cho đọc
CRR insteadNên dùng CRR
- Dynamic content that must be fresh in few regionsContent động phải luôn mới ở vài region
- Compliance says "data must live in region X"Compliance bắt "dữ liệu phải nằm ở region X"
- Reads only, and TTL-caching would show stale dataChỉ đọc, TTL cache sẽ trả dữ liệu cũ
OriginsOrigin
S3 (with OAC) · MediaPackage · VPC origin (private ALB/NLB/EC2) · Custom HTTP. S3 (với OAC) · MediaPackage · VPC origin (ALB/NLB/EC2 private) · Custom HTTP.
Signed URL — CloudFront vs S3Signed URL — CloudFront vs S3
| CloudFront signed URL | S3 pre-signed URL | |
|---|---|---|
| Grants access toCấp quyền cho | A path — any origin behind CFMột path — bất kỳ origin sau CF | A specific objectObject cụ thể |
| Identity usedDanh tính | Public key in a trusted key group (IAM-managed; legacy account key pair needs root)Public key trong trusted key group (quản bằng IAM; key pair account kiểu cũ cần root) | IAM identity of the signerIAM của người ký |
| FiltersFilter | IP · path · date · expirationIP · path · date · hết hạn | Just expirationChỉ có expiration |
| CachingCache | ✅ | ❌ (direct-to-S3)(vào thẳng S3) |
Edge compute — Functions vs Lambda@EdgeEdge compute — Functions vs Lambda@Edge
Header/URL rewrite & A/B routing → Functions. Anything talking to origin, using SDKs, or heavier → Lambda@Edge. Rewrite header/URL & route A/B → Functions. Có gọi origin, dùng SDK, hoặc nặng hơn → Lambda@Edge.
| CloudFront Functions | Lambda@Edge | |
|---|---|---|
| RuntimeRuntime | JavaScript | Node.js · Python |
| Where it runsChạy ở đâu | Edge LocationEdge Location | Regional Edge CacheRegional Edge Cache |
| IsolationIsolation | Process-basedProcess-based | VM-basedVM-based |
| ScaleScale | Millions req/sHàng triệu req/s | 10k req/s per Region10k req/s mỗi Region |
| HooksHook | Viewer Req · Viewer RespViewer Req · Viewer Resp | Viewer Req/Resp + Origin Req/RespViewer Req/Resp + Origin Req/Resp |
| Network / FSMạng / FS | ❌ | ✅ |
| KeyValueStoreKeyValueStore | ✅ (JS runtime 2.0) — low-latency key-value lookups, 5 MB/store✅ (JS runtime 2.0) — tra key-value độ trễ thấp, 5 MB/store | ❌ |
CloudFront-Viewer-Country + geo/device headers work in CloudFront Functions and in Lambda@Edge origin hooks — not in Lambda@Edge viewer hooks. Add the header via a cache / origin request policy first.
CloudFront-Viewer-Country + header geo/device dùng được trong CloudFront Functions và hook origin của Lambda@Edge — không có ở hook viewer của Lambda@Edge. Nhớ thêm header qua cache / origin request policy trước.
Locking the originChặn origin
- S3 origin — use OAC and a bucket policy that only allows requests from the CF distribution's service principal. S3 origin — dùng OAC và bucket policy chỉ cho phép request từ service principal của distribution.
- ALB / EC2 origin — CF adds a secret custom header; ALB WAF/listener rule drops any request without it. ALB / EC2 origin — CF gắn custom header bí mật; ALB WAF/listener rule loại request thiếu header đó.
-
Alternative: security group only accepts CloudFront's published IP ranges (managed prefix list
com.amazonaws.global.cloudfront.origin-facing). Cách khác: security group chỉ nhận dải IP CloudFront công bố (managed prefix listcom.amazonaws.global.cloudfront.origin-facing).
Geo restrict & price classGeo restrict & price class
Origin failover — the read-only footnoteOrigin failover — dòng chú thích "chỉ đọc"
An origin group is the native way to survive a dead origin — including one in another Region — and it's usually the right answer when the stem says "highly available and fault tolerant" about a CloudFront-fronted app. But it fails over per request, and only for reads. Một origin group là cách native để sống sót khi origin chết — kể cả origin ở Region khác — và thường là đáp án đúng khi đề nói "highly available and fault tolerant" về một app có CloudFront phía trước. Nhưng nó failover theo từng request, và chỉ cho các request đọc.
www.example.com
└── Route 53 A (Alias) ──→ MỘT CloudFront distribution
└── origin group
├── primary : ALB Region A (+ ASG, multi-AZ)
└── secondary : ALB Region B (+ ASG, multi-AZ)
failover khi: 500 · 502 · 503 · 504 · connection timeout … (tự chọn mã)
GET · HEAD · OPTIONS onlymà thôi
CloudFront deliberately will not retry POST, PUT, PATCH or DELETE against the secondary origin — replaying a write could duplicate a payment. It also doesn't mark the primary as dead; the next request tries the primary again. So an origin group is a read resilience feature, not a regional evacuation. For write-heavy failover, use Global Accelerator or Route 53 failover in front of the load balancers, with a data-replication story to match.
CloudFront cố ý không retry POST, PUT, PATCH hay DELETE sang origin phụ — phát lại một lệnh ghi có thể tạo giao dịch trùng. Nó cũng không đánh dấu primary là chết; request kế tiếp lại thử primary. Nên origin group là tính năng chịu lỗi cho đọc, không phải cách sơ tán cả Region. Muốn failover cho workload nhiều ghi, dùng Global Accelerator hoặc Route 53 failover đặt trước các load balancer, kèm một phương án replicate dữ liệu tương xứng.
- Keep one distribution. Building a second distribution and failing over between them with Route 53 is worse in every way: two caches, two configs, two certificates — and the same alternate domain name generally can't sit on both.Giữ một distribution. Dựng distribution thứ hai rồi failover giữa chúng bằng Route 53 là tệ hơn ở mọi mặt: hai cache, hai cấu hình, hai certificate — và cùng một alternate domain name thường không đặt được lên cả hai.
- An ALB is Regional. You cannot register an ASG or instances from another Region into its target group, and there is no "failover routing algorithm" on a load balancer. Each Region needs its own ALB + ASG.ALB là Regional. Không đăng ký được ASG hay instance từ Region khác vào target group của nó, và load balancer không có "failover routing algorithm". Mỗi Region phải có ALB + ASG riêng.
- Global Accelerator cannot take a CloudFront distribution as an endpoint — its endpoints are ALB, NLB, EC2 and Elastic IP. CloudFront is already a global entry point.Global Accelerator không nhận CloudFront distribution làm endpoint — endpoint của nó là ALB, NLB, EC2 và Elastic IP. CloudFront vốn đã là điểm vào toàn cầu.
- An origin must be a DNS endpoint — an ALB, an S3 bucket, a custom HTTP host, a Lambda function URL. A target group is not an origin.Origin phải là một DNS endpoint — một ALB, một S3 bucket, một HTTP host tuỳ ý, một Lambda function URL. Một target group không phải origin.
OAC vsvs OAI
| OAI (legacycũ) | OAC (use thisdùng cái này) | |
|---|---|---|
| MechanismCơ chế | a special CloudFront identitymột identity riêng của CloudFront | SigV4-signed requests to the originrequest tới origin được ký SigV4 |
| SSE-KMS objectsobject | awkward / limitedvướng / hạn chế | ✓ |
PUT · POST · DELETE | ✗ | ✓ |
| Origin typesLoại origin | S3 | S3 · Lambda function URL · MediaStore · MediaPackage v2S3 · Lambda function URL · MediaStore · MediaPackage v2 |
// bucket policy phía origin — chỉ đúng distribution này được đọc
{ "Effect": "Allow",
"Principal": { "Service": "cloudfront.amazonaws.com" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-bucket/*",
"Condition": { "StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::123456789012:distribution/ABC123" } } }
bucket.s3.<region>.amazonaws.com is an S3 origin — OAC works, the bucket stays private. bucket.s3-website-<region>.amazonaws.com is a custom HTTP origin as far as CloudFront is concerned, so OAC does not apply and the bucket must be public. If you need index documents and redirect rules and a private bucket, use the S3 origin with a CloudFront Function or a default root object instead of the website endpoint.
bucket.s3.<region>.amazonaws.com là S3 origin — OAC hoạt động, bucket giữ private. bucket.s3-website-<region>.amazonaws.com theo cách nhìn của CloudFront là custom HTTP origin, nên OAC không áp dụng và bucket buộc phải public. Nếu cần index document và redirect rule mà vẫn muốn bucket private, dùng S3 origin kèm CloudFront Function hoặc default root object thay cho website endpoint.