AWS DVA-C02
Esc
↑↓เลื่อน↵เปิดescปิด28 ผลลัพธ์
12

CloudFront

CDN, edge compute & smart routing

Networking & Content Delivery 10 นาที 22 คำถาม

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
Edge Location vs Regional Edge Cache
CloudFront มีสองชั้น: Edge Locations (200+ จุด, ใกล้ผู้ใช้ที่สุด, รัน CloudFront Functions) และ Regional Edge Caches (ใหญ่กว่า, cache นานกว่า, รัน Lambda@Edge) ช่วยเพิ่ม cache hit ก่อนถึง origin

02Origins: S3 + OAC/OAI และ Custom Origin

Origin คือแหล่งเนื้อหาต้นทางที่ CloudFront ดึงมา cache โดย CloudFront รองรับ origin ได้หลายประเภท ทั้ง static content จาก S3 และ dynamic content จาก HTTP backend

S3 bucket (เป็น origin)
ใช้สำหรับ distribute static files และ cache ที่ edge ใช้คู่กับ Origin Access Control (OAC) หรือ OAI เพื่อให้เข้าถึง S3 ผ่าน CloudFront เท่านั้น CloudFront ยังใช้ upload ไป S3 ได้ด้วย
Custom Origin (HTTP)
เช่น Application Load Balancer, EC2 instance, S3 static website endpoint, API Gateway, หรือ HTTP backend อื่นๆ ใช้สำหรับ dynamic content

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 ใหม่ไม่รองรับ
json
{
  "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"
      }
    }
  }]
}
Tips สำหรับการสอบ
ถ้าโจทย์ถาม "AWS recommended way" ให้ S3 เข้าถึงผ่าน CloudFront เท่านั้น -> ตอบ OAC ถ้าโจทย์มี SSE-KMS -> ต้องเป็น OAC เพราะ OAI ไม่รองรับ KMS

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 แตกเป็นหลายเวอร์ชัน
กับดักข้อสอบ: Cache Hit Ratio
ยิ่งเพิ่ม headers/cookies/query strings เข้าไปใน Cache Key มาก ยิ่งทำให้ object ถูกแยก cache เป็นหลายเวอร์ชัน ส่งผลให้ cache hit ratio ลดลง และ load ที่ origin เพิ่มขึ้น ถ้าโจทย์อยากเพิ่ม hit ratio ให้ลดองค์ประกอบใน cache key

04TTL & Cache Invalidation

TTL (Time To Live) กำหนดว่า object จะอยู่ใน cache นานเท่าใดก่อนต้องไป validate กับ origin origin ควบคุม TTL ได้ผ่าน header Cache-Control: max-age หรือ Expires

Minimum / Maximum / Default TTL
Minimum TTL (default 0): ค่าต่ำสุดที่บังคับ Maximum TTL (default 31536000 วิ = 1 ปี): ค่าสูงสุดที่อนุญาต Default TTL (default 86400 วิ = 1 วัน): ใช้เมื่อ origin ไม่ส่ง Cache-Control header

Cache Invalidation

เมื่อ deploy เนื้อหาใหม่แต่ผู้ใช้ยังเห็นของเก่าใน cache สามารถสร้าง Invalidation เพื่อบังคับลบ object ออกจาก edge ได้ทันที โดยไม่ต้องรอ TTL หมด สามารถ invalidate ทั้งหมดด้วย /* หรือเฉพาะ path เช่น /images/*

bash
# Invalidate ทั้ง distribution
aws cloudfront create-invalidation \
  --distribution-id EDFDVBD6EXAMPLE \
  --paths "/*"

# Invalidate เฉพาะบาง path
aws cloudfront create-invalidation \
  --distribution-id EDFDVBD6EXAMPLE \
  --paths "/images/*" "/index.html"
Tips สำหรับการสอบ
ถ้าโจทย์ว่า "deploy แล้วผู้ใช้ยังเห็นไฟล์เก่า ต้องการเห็นใหม่ทันที" -> ตอบ CloudFront Invalidation อีกเทคนิคที่นิยมคือ versioned file name (เช่น app.v2.js) ซึ่งไม่ต้อง invalidate เลย

05Signed URLs vs Signed Cookies

ทั้ง Signed URL และ Signed Cookies ใช้ให้สิทธิ์เข้าถึงเนื้อหาแบบ restricted/premium content โดยกำหนด policy เช่น expiration, IP range, trusted signers ได้ ทั้งคู่ sign ด้วย private key ของ trusted key group (วิธีแนะนำ)

ประเด็นSigned URLSigned Cookies
ขอบเขต1 URL = 1 ไฟล์หลายไฟล์พร้อมกัน
เปลี่ยน URL?ต้องสร้าง URL ใหม่ต่อไฟล์ไม่ต้องเปลี่ยน URL
เหมาะกับดาวน์โหลดไฟล์เดี่ยวstreaming หลาย segment
ฝั่ง clientใช้ใน RTMP/legacy ได้ต้องรองรับ cookies
  • Signed URL = ให้สิทธิ์ individual file (one file = one URL)
  • Signed Cookies = ให้สิทธิ์ multiple files โดยไม่ต้องเปลี่ยน URL แต่ละไฟล์
  • Trusted Key Group (แนะนำ) สร้างและหมุน key ได้โดยไม่ต้องใช้ root account ต่างจาก legacy CloudFront Key Pair ที่ต้องใช้ root
กับดัก: Signed URL ของ CloudFront vs S3 Pre-signed URL
อย่าสับสน! CloudFront Signed URL ให้สิทธิ์ผ่าน edge ครอบคลุมทุก origin และ leverage caching ส่วน S3 Pre-signed URL ใช้ IAM identity ของผู้สร้างและเข้าถึง S3 โดยตรงเท่านั้น ถ้าโจทย์มี CloudFront อยู่ในภาพ ให้เลือก CloudFront Signed URL

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
กับดักข้อสอบ: ACM ต้องอยู่ us-east-1
CloudFront เป็น global service ดังนั้น ACM certificate สำหรับ custom domain ต้อง provision ที่ region us-east-1 (N. Virginia) เท่านั้น ไม่ว่า origin จะอยู่ region ไหน certificate ที่ region อื่นจะไม่ขึ้นให้เลือก นี่ออกสอบบ่อยมาก

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 ได้ปกติ
Origin Group + S3 CRR
รูปแบบที่นิยม: primary = S3 bucket ใน region A, secondary = S3 bucket ใน region B (replicate ด้วย S3 Cross-Region Replication) ทำให้ static content มี HA แบบ multi-region ผ่าน CloudFront

08Lambda@Edge vs CloudFront Functions

ทั้งสองคือ edge compute ที่รันโค้ดใกล้ผู้ใช้เพื่อปรับแต่ง request/response แต่ต่างกันมากในเรื่อง scale, ความสามารถ และจุดที่ทำงาน

CloudFront Functions
เขียนด้วย JavaScript (ECMAScript) รันที่ Edge Locations (200+ จุด) มี sub-millisecond startup, scale ถึง ล้าน req/sec, memory จำกัดมาก (~2MB), ไม่มี network/file access ทำงานได้แค่ viewer request/response เหมาะงานเบา
Lambda@Edge
เขียนด้วย Node.js หรือ Python รันที่ Regional Edge Caches scale ถึงพันๆ req/sec มี memory/เวลามากกว่า มี network access ใช้ third-party libraries ได้ ทำงานได้ทั้ง 4 จุด เหมาะงานซับซ้อน
ประเด็นCloudFront FunctionsLambda@Edge
ภาษาJavaScript เท่านั้นNode.js, Python
รันที่Edge LocationsRegional Edge Caches
Trigger pointsviewer req/res (2)ทั้ง 4 จุด
Network accessไม่มีมี
Scaleล้าน req/secพันๆ req/sec
Use caseheader/URL rewrite, JWT, redirectrender dynamic, call API, A/B test
javascript
// 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;
}
Tips สำหรับการสอบ
คำสำคัญ: header manipulation, URL rewrite/redirect, JWT validation, cache key normalization, high scale ถูก latency ต่ำสุด -> CloudFront Functions ส่วน ต้อง network access, call DynamoDB/API, นาน, Python, ทำที่ origin request/response -> Lambda@Edge

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
ทำไม static IP ถึงสำคัญ
องค์กรที่ต้อง whitelist IP บน firewall ของลูกค้าจะชอบ Global Accelerator เพราะให้ 2 static IP คงที่ ไม่ต้องอัปเดต whitelist เมื่อ scale ส่วน CloudFront ใช้ DNS name ไม่มี static IP ตายตัว

10CloudFront vs Global Accelerator (สรุปเปรียบเทียบ)

นี่คือคู่เปรียบเทียบที่ ออกสอบบ่อยที่สุด ของหัวข้อนี้ ทั้งคู่ใช้ AWS edge network เหมือนกันแต่จุดประสงค์ต่างกันสิ้นเชิง

ประเด็นCloudFrontGlobal Accelerator
ประเภทCDN (caching)Network proxy (no cache)
Cachingcache ที่ edgeไม่ cache เลย
ProtocolHTTP/HTTPSTCP/UDP (รวม HTTP)
IPDNS name2 static anycast IP
Use casestatic/dynamic web contentgaming, IoT, VoIP, static IP, non-HTTP
Content servedที่ edgeroute ไป origin/endpoint
Tips สำหรับการสอบ
คำสำคัญ: cache content, web/HTTP, static website, media (HLS/DASH) -> CloudFront คำสำคัญ: UDP/TCP non-HTTP, gaming, static IP whitelist, ไม่ cache, regional failover เร็ว -> Global Accelerator ถ้าโจทย์ "serve cacheable web content fast" เลือก CloudFront เสมอ
  • ทั้งคู่ได้ประโยชน์จาก AWS global private network (latency ต่ำ, เสถียร)
  • ทั้งคู่มี DDoS protection ผ่าน AWS Shield
  • CloudFront เน้น content caching; Global Accelerator เน้น smart routing + static IP

คำถามทบทวน

ทดสอบความเข้าใจหัวข้อ CloudFront

22 ข้อ · เลือกคำตอบเพื่อดูเฉลยและคำอธิบาย

ทำแล้ว 0/22คะแนน 0
  1. 1.บริษัทใช้ S3 bucket เป็น origin ของ CloudFront และต้องการให้ผู้ใช้เข้าถึงไฟล์ผ่าน CloudFront เท่านั้น ไม่สามารถเข้า S3 โดยตรงได้ วิธีใดที่ AWS แนะนำในปัจจุบัน?

  2. 2.ต้องการให้ผู้ใช้แต่ละคนเข้าถึงไฟล์เดียวใน CloudFront โดยจำกัดเวลาและ IP ควรใช้อะไร?

  3. 3.แอปต้องการแก้ไข HTTP response header ทุก request ด้วย latency ต่ำที่สุดและไม่ต้องเข้าถึง network หรือไฟล์ระบบ ควรใช้อะไร?

  4. 4.ข้อใดคือความแตกต่างหลักระหว่าง CloudFront Functions และ Lambda@Edge?

  5. 5.ต้องการให้บริการเฉพาะผู้ใช้ในประเทศไทยและบล็อกประเทศอื่นบน CloudFront ควรใช้ฟีเจอร์ใด?

  6. 6.Origin Group ใน CloudFront ใช้สำหรับอะไร?

  7. 7.หลังจาก deploy เนื้อหาใหม่ขึ้น origin แต่ผู้ใช้ยังเห็นไฟล์เก่าเพราะ cache ยังไม่หมดอายุ ต้องการให้เห็นเวอร์ชันใหม่ทันที ควรทำอย่างไร?

  8. 8.AWS Global Accelerator ต่างจาก CloudFront อย่างไร?

  9. 9.จุดเด่นของ AWS Global Accelerator ในเรื่อง IP address คืออะไร?

  10. 10.Cache Key ใน CloudFront คืออะไร?

  11. 11.ค่า TTL ใน CloudFront ถูกควบคุมอย่างไรเมื่อ origin ไม่ส่ง Cache-Control header?

  12. 12.แอปวิดีโอสตรีมมิ่งต้องการให้ผู้ใช้ที่ subscribe เข้าถึงไฟล์วิดีโอหลายไฟล์ในโฟลเดอร์เดียวกันได้โดยไม่ต้อง sign แต่ละ URL ควรใช้อะไร?

  13. 13.เปรียบเทียบ CloudFront กับ S3 Cross-Region Replication ข้อใดถูกต้อง?

  14. 14.ต้องการใช้ HTTPS กับ CloudFront โดยใช้ custom domain เช่น www.example.com ต้อง provision ACM certificate ที่ region ใด?

  15. 15.Lambda@Edge สามารถทำงานที่จุดใดบ้างใน CloudFront request lifecycle?

  16. 16.Global Accelerator มี health check ที่ระดับใดและทำอะไรเมื่อ endpoint ไม่ healthy?

  17. 17.การเพิ่ม query strings และ headers จำนวนมากเข้าไปใน Cache Key มีผลอย่างไร?

  18. 18.CloudFront รองรับ origin ประเภทใดได้บ้าง?

  19. 19.เกมออนไลน์ต้องการ low latency global พร้อม UDP traffic และ static IP เพื่อ whitelist บน firewall ของลูกค้า ควรใช้บริการใด?

  20. 20.CloudFront Functions มีขีดจำกัด memory และ execution อย่างไรเมื่อเทียบกับ Lambda@Edge?

  21. 21.ต้องการป้องกัน hotlinking และให้สิทธิ์เข้าถึงเนื้อหาเฉพาะช่วงเวลาที่กำหนด CloudFront Signed URL ใช้ key อะไรในการ sign?

  22. 22.distribution แบบใดของ CloudFront เหมาะกับการ stream วิดีโอและ static website ในปัจจุบัน?