CloudFront
CDN, edge compute & smart routing
01CloudFront คืออะไร & Edge Locations
Amazon CloudFront คือ Content Delivery Network (CDN) ของ AWS ที่ช่วย cache เนื้อหา ไว้ที่จุดใกล้ผู้ใช้ทั่วโลก ทำให้ read latency ต่ำลงและเพิ่มประสบการณ์ผู้ใช้ ปัจจุบัน CloudFront มี edge locations มากกว่า 200+ จุด ทั่วโลก
หลักการทำงาน: ผู้ใช้ขอเนื้อหา -> request ไปยัง edge location ที่ใกล้ที่สุด -> ถ้ามีใน cache (cache hit) ส่งกลับทันที -> ถ้าไม่มี (cache miss) edge จะดึงจาก origin มา cache ไว้แล้วส่งกลับ
- DDoS protection ในตัว (ผสานกับ AWS Shield และ AWS WAF)
- ช่วย ลด load ที่ origin เพราะ cache ที่ edge
- รองรับ HTTPS และ custom SSL certificate
- ต่างจาก S3 Cross-Region Replication: CloudFront cache แบบ global มี TTL ส่วน CRR replicate ถาวรในไม่กี่ region
02Origins: S3 + OAC/OAI และ Custom Origin
Origin คือแหล่งเนื้อหาต้นทางที่ CloudFront ดึงมา cache โดย CloudFront รองรับ origin ได้หลายประเภท ทั้ง static content จาก S3 และ dynamic content จาก HTTP backend
OAC vs OAI (สำคัญมาก)
Origin Access Control (OAC) คือวิธี ใหม่ที่ AWS แนะนำ ในปัจจุบัน ทดแทน Origin Access Identity (OAI) ซึ่งเป็น legacy ทั้งคู่มีเป้าหมายเดียวกันคือล็อก S3 ให้เข้าถึงได้ผ่าน CloudFront เท่านั้น โดยใช้ S3 bucket policy อนุญาตเฉพาะ CloudFront
| ประเด็น | OAC (แนะนำ) | OAI (legacy) |
|---|---|---|
| สถานะ | ใหม่ AWS แนะนำ | เก่า ยังใช้ได้ |
| SSE-KMS encryption | รองรับเต็มที่ | ไม่รองรับ |
| HTTP methods | รองรับ PUT/POST/DELETE | จำกัด GET/HEAD เป็นหลัก |
| AWS Regions | ทุก region รวม opt-in | บาง region ใหม่ไม่รองรับ |
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": { "Service": "cloudfront.amazonaws.com" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE"
}
}
}]
}03Cache Behaviors & Cache Key
Cache Behaviors ให้คุณกำหนดการตั้งค่าที่ต่างกันตาม URL path pattern เช่น path /images/* ใช้ origin หนึ่ง และ /api/* ใช้อีก origin หนึ่ง โดยมี **Default(*) Cache Behavior** ที่ทำงานเมื่อไม่ตรง pattern อื่น (precedence ต่ำสุด)
Cache Key คือ unique identifier ที่ CloudFront ใช้ระบุแต่ละ object ใน cache โดย default จะประกอบด้วย hostname + resource path (และ query string) สามารถเพิ่ม HTTP headers, cookies, query strings เข้าไปใน cache key ได้ผ่าน Cache Policy
- Cache Policy: ควบคุมว่าอะไรอยู่ใน cache key และ TTL
- Origin Request Policy: ควบคุมว่าจะส่งอะไรไป origin (ไม่กระทบ cache key)
- ใช้ separate policy เพื่อส่งข้อมูลไป origin โดยไม่ทำให้ cache แตกเป็นหลายเวอร์ชัน
04TTL & Cache Invalidation
TTL (Time To Live) กำหนดว่า object จะอยู่ใน cache นานเท่าใดก่อนต้องไป validate กับ origin origin ควบคุม TTL ได้ผ่าน header Cache-Control: max-age หรือ Expires
Cache Invalidation
เมื่อ deploy เนื้อหาใหม่แต่ผู้ใช้ยังเห็นของเก่าใน cache สามารถสร้าง Invalidation เพื่อบังคับลบ object ออกจาก edge ได้ทันที โดยไม่ต้องรอ TTL หมด สามารถ invalidate ทั้งหมดด้วย /* หรือเฉพาะ path เช่น /images/*
# Invalidate ทั้ง distribution
aws cloudfront create-invalidation \
--distribution-id EDFDVBD6EXAMPLE \
--paths "/*"
# Invalidate เฉพาะบาง path
aws cloudfront create-invalidation \
--distribution-id EDFDVBD6EXAMPLE \
--paths "/images/*" "/index.html"06Geo Restriction & HTTPS/SSL
Geo Restriction
Geo Restriction จำกัดการเข้าถึงตาม ประเทศ ได้ที่ระดับ distribution มีสองแบบ: Allowlist (อนุญาตเฉพาะประเทศที่ระบุ) และ Blocklist (บล็อกประเทศที่ระบุ) ใช้ third-party GeoIP database ในการระบุประเทศ เหมาะกับเรื่อง copyright/compliance
HTTPS / SSL
- Viewer Protocol Policy: บังคับ HTTPS เช่น Redirect HTTP to HTTPS หรือ HTTPS Only
- Origin Protocol Policy: กำหนดวิธีคุยกับ origin (HTTP only / HTTPS only / Match Viewer)
- ใช้ custom SSL certificate ผ่าน ACM สำหรับ custom domain
07Origin Groups & High Availability
Origin Group ใช้เพิ่ม high availability ด้วยการทำ origin failover ประกอบด้วย primary origin และ secondary origin เมื่อ primary ตอบ status code ที่กำหนดไว้ CloudFront จะ failover ไป secondary อัตโนมัติ
- Failover status codes ที่กำหนดได้ เช่น 500, 502, 503, 504 (server errors) และ 403, 404, 416 (บางกรณี)
- ใช้คู่กับ S3 + S3 (cross-region) หรือ EC2/ALB เพื่อ resilience
- ทำงานร่วมกับ CloudFront caching ได้ปกติ
08Lambda@Edge vs CloudFront Functions
ทั้งสองคือ edge compute ที่รันโค้ดใกล้ผู้ใช้เพื่อปรับแต่ง request/response แต่ต่างกันมากในเรื่อง scale, ความสามารถ และจุดที่ทำงาน
| ประเด็น | CloudFront Functions | Lambda@Edge |
|---|---|---|
| ภาษา | JavaScript เท่านั้น | Node.js, Python |
| รันที่ | Edge Locations | Regional Edge Caches |
| Trigger points | viewer req/res (2) | ทั้ง 4 จุด |
| Network access | ไม่มี | มี |
| Scale | ล้าน req/sec | พันๆ req/sec |
| Use case | header/URL rewrite, JWT, redirect | render dynamic, call API, A/B test |
// CloudFront Function: redirect และ add security header (viewer response)
function handler(event) {
var response = event.response;
var headers = response.headers;
headers['strict-transport-security'] = {
value: 'max-age=63072000; includeSubdomains; preload'
};
headers['x-content-type-options'] = { value: 'nosniff' };
return response;
}09AWS Global Accelerator
AWS Global Accelerator ช่วยเพิ่ม performance และ availability ของ application โดย route traffic ผ่าน AWS global network ไปยัง endpoint ที่ใกล้ที่สุด จุดเด่นคือให้ 2 static anycast IP addresses เป็น entry point คงที่
- ใช้ anycast IP: ผู้ใช้ถูก route ไป edge location ที่ใกล้ที่สุด แล้ววิ่งผ่าน AWS backbone
- ไม่ cache เนื้อหา (ต่างจาก CloudFront) เป็นเพียง network proxy
- รองรับ TCP และ UDP เหมาะกับ non-HTTP เช่น gaming, IoT, VoIP
- มี health check + automatic failover ไป healthy endpoint ภายในไม่กี่วินาที
- endpoint ได้แก่ ALB, NLB, EC2, Elastic IP
10CloudFront vs Global Accelerator (สรุปเปรียบเทียบ)
นี่คือคู่เปรียบเทียบที่ ออกสอบบ่อยที่สุด ของหัวข้อนี้ ทั้งคู่ใช้ AWS edge network เหมือนกันแต่จุดประสงค์ต่างกันสิ้นเชิง
| ประเด็น | CloudFront | Global Accelerator |
|---|---|---|
| ประเภท | CDN (caching) | Network proxy (no cache) |
| Caching | cache ที่ edge | ไม่ cache เลย |
| Protocol | HTTP/HTTPS | TCP/UDP (รวม HTTP) |
| IP | DNS name | 2 static anycast IP |
| Use case | static/dynamic web content | gaming, IoT, VoIP, static IP, non-HTTP |
| Content served | ที่ edge | route ไป origin/endpoint |
- ทั้งคู่ได้ประโยชน์จาก AWS global private network (latency ต่ำ, เสถียร)
- ทั้งคู่มี DDoS protection ผ่าน AWS Shield
- CloudFront เน้น content caching; Global Accelerator เน้น smart routing + static IP
คำถามทบทวน
ทดสอบความเข้าใจหัวข้อ CloudFront
22 ข้อ · เลือกคำตอบเพื่อดูเฉลยและคำอธิบาย
1.บริษัทใช้ S3 bucket เป็น origin ของ CloudFront และต้องการให้ผู้ใช้เข้าถึงไฟล์ผ่าน CloudFront เท่านั้น ไม่สามารถเข้า S3 โดยตรงได้ วิธีใดที่ AWS แนะนำในปัจจุบัน?
2.ต้องการให้ผู้ใช้แต่ละคนเข้าถึงไฟล์เดียวใน CloudFront โดยจำกัดเวลาและ IP ควรใช้อะไร?
3.แอปต้องการแก้ไข HTTP response header ทุก request ด้วย latency ต่ำที่สุดและไม่ต้องเข้าถึง network หรือไฟล์ระบบ ควรใช้อะไร?
4.ข้อใดคือความแตกต่างหลักระหว่าง CloudFront Functions และ Lambda@Edge?
5.ต้องการให้บริการเฉพาะผู้ใช้ในประเทศไทยและบล็อกประเทศอื่นบน CloudFront ควรใช้ฟีเจอร์ใด?
6.Origin Group ใน CloudFront ใช้สำหรับอะไร?
7.หลังจาก deploy เนื้อหาใหม่ขึ้น origin แต่ผู้ใช้ยังเห็นไฟล์เก่าเพราะ cache ยังไม่หมดอายุ ต้องการให้เห็นเวอร์ชันใหม่ทันที ควรทำอย่างไร?
8.AWS Global Accelerator ต่างจาก CloudFront อย่างไร?
9.จุดเด่นของ AWS Global Accelerator ในเรื่อง IP address คืออะไร?
10.Cache Key ใน CloudFront คืออะไร?
11.ค่า TTL ใน CloudFront ถูกควบคุมอย่างไรเมื่อ origin ไม่ส่ง Cache-Control header?
12.แอปวิดีโอสตรีมมิ่งต้องการให้ผู้ใช้ที่ subscribe เข้าถึงไฟล์วิดีโอหลายไฟล์ในโฟลเดอร์เดียวกันได้โดยไม่ต้อง sign แต่ละ URL ควรใช้อะไร?
13.เปรียบเทียบ CloudFront กับ S3 Cross-Region Replication ข้อใดถูกต้อง?
14.ต้องการใช้ HTTPS กับ CloudFront โดยใช้ custom domain เช่น www.example.com ต้อง provision ACM certificate ที่ region ใด?
15.Lambda@Edge สามารถทำงานที่จุดใดบ้างใน CloudFront request lifecycle?
16.Global Accelerator มี health check ที่ระดับใดและทำอะไรเมื่อ endpoint ไม่ healthy?
17.การเพิ่ม query strings และ headers จำนวนมากเข้าไปใน Cache Key มีผลอย่างไร?
18.CloudFront รองรับ origin ประเภทใดได้บ้าง?
19.เกมออนไลน์ต้องการ low latency global พร้อม UDP traffic และ static IP เพื่อ whitelist บน firewall ของลูกค้า ควรใช้บริการใด?
20.CloudFront Functions มีขีดจำกัด memory และ execution อย่างไรเมื่อเทียบกับ Lambda@Edge?
21.ต้องการป้องกัน hotlinking และให้สิทธิ์เข้าถึงเนื้อหาเฉพาะช่วงเวลาที่กำหนด CloudFront Signed URL ใช้ key อะไรในการ sign?
22.distribution แบบใดของ CloudFront เหมาะกับการ stream วิดีโอและ static website ในปัจจุบัน?