Amazon CloudFront
CDN, cache gần người dùng
Mạng phân phối nội dung: cache file và API ở hàng trăm điểm biên (edge) gần người dùng để giảm độ trễ.
- Chi phí$ Rẻ
- Độ khó●●●●●
- Để không có tốn tiền?Không
- AWS quản lý
- Có trong đềCLF · SAA · DVA
Hình dungNhư mở chi nhánh cửa hàng ở mỗi quận thay vì bắt khách về tổng kho.
CloudFront đứng ở đâu trong hệ thống?
Gọi tới CloudFront
- Người dùnggửi request
Route 53trỏ tên miền tới
WAFlọc request cho
ACMcấp HTTPS cho
CloudFront gọi tới
Một ví dụ thật, từng bước
- Một website bán khoá học đặt máy chủ ở Singapore, người dùng ở Nhật và Mỹ thấy tải trang khá chậm.
- Bạn tạo CloudFront distribution: ảnh và video lấy từ bucket S3, còn trang động lấy từ ALB.
- Lần đầu người dùng ở Tokyo mở trang, CloudFront lấy file từ Singapore rồi giữ lại ở điểm biên gần Tokyo.
- Những người ở Tokyo vào sau nhận file ngay từ điểm biên đó, nhanh hơn nhiều và máy chủ gốc đỡ tải.
- Video trả phí được bảo vệ bằng signed URL; bucket S3 để riêng tư, chỉ CloudFront đọc được qua Origin Access Control.
Khi nào nên dùng
✓ Nên dùng khi
Website, video, API có người dùng ở nhiều nơi.
✗ Không nên dùng khi
Cần tăng tốc giao thức không phải HTTP → Global Accelerator.
Ví dụ hay gặp
- Tăng tốc website/ứng dụng cho người dùng toàn cầu
- Phát video, tải file lớn
- Đặt trước S3 để giấu bucket (OAC) và giảm phí truyền dữ liệu
- Chạy logic ở edge (CloudFront Functions, Lambda@Edge)
CloudFront, S3 hay ELB?
| Làm gì | Cache và phân phối nội dung toàn cầu | Lưu file | Chia traffic trong một region |
|---|---|---|---|
| HTTPS với tên miền riêng | Có (chứng chỉ ACM ở us-east-1) | Website endpoint chỉ HTTP | Có (chứng chỉ ACM cùng region) |
| Gần người dùng | Hàng trăm điểm biên toàn cầu | Một region | Một region |
| Hay dùng cùng | S3, ALB, WAF, Lambda@Edge | CloudFront | Auto Scaling, CloudFront |
Tốn bao nhiêu tiền?
- Cách tính: Theo GB truyền tới người dùng (~$0,085/GB ở Bắc Mỹ/châu Âu, khác theo vùng) + số request HTTPS (~$0,01/10.000). Dữ liệu từ origin AWS về CloudFront miễn phí.
- Miễn phí: Always Free: 1 TB truyền dữ liệu + 10 triệu request mỗi tháng.
Tỉ lệ cache hit thấp (cache key quá chi tiết) đẩy tải về origin; chọn Price Class 'All' khi người dùng chủ yếu ở một vùng.
Chỉ cho người dùng đọc S3 qua CloudFront → Origin Access Control + bucket policy. Nội dung trả phí → signed URL hoặc signed cookie. Chứng chỉ cho CloudFront phải tạo ở us-east-1. Cần tăng tốc TCP/UDP không phải HTTP → Global Accelerator.
Thử sức với câu hỏi thi thật
1A company wants to configure its Amazon CloudFront distribution to use SSL/TLS certificates. The company does not want to use the default domain name for the distribution. Instead, the company wants to use a different domain name for the distribution. Which solution will deploy the certificate without incurring any additional costs?
- ARequest an Amazon issued private certificate from AWS Certificate Manager (ACM) in the us-east-1 Region.
- BRequest an Amazon issued private certificate from AWS Certificate Manager (ACM) in the us-west-1 Region.
- CRequest an Amazon issued public certificate from AWS Certificate Manager (ACM) in the us-east-1 Region.
- DRequest an Amazon issued public certificate from AWS Certificate Manager (ACM) in the us-west-1 Region.
Xem đáp án
Đáp án đúng: C
Chứng chỉ public của ACM miễn phí, và CloudFront chỉ đọc chứng chỉ ở region us-east-1. Chứng chỉ private (Private CA) thì tính phí.
2An ecommerce company wants to launch a one-deal-a-day website on AWS. Each day will feature exactly one product on sale for a period of 24 hours. The company wants to be able to handle millions of requests each hour with millisecond latency during peak hours. Which solution will meet these requirements with the LEAST operational overhead?
- AUse Amazon S3 to host the full website in different S3 buckets. Add Amazon CloudFront distributions. Set the S3 buckets as origins for the distributions. Store the order data in Amazon S3.
- BDeploy the full website on Amazon EC2 instances that run in Auto Scaling groups across multiple Availability Zones. Add an Application Load Balancer (ALB) to distribute the website traffic. Add another ALB for the backend APIs. Store the data in Amazon RDS for MySQL.
- CMigrate the full application to run in containers. Host the containers on Amazon Elastic Kubernetes Service (Amazon EKS). Use the Kubernetes Cluster Autoscaler to increase and decrease the number of pods to process bursts in traffic. Store the data in Amazon RDS for MySQL.
- DUse an Amazon S3 bucket to host the website's static content. Deploy an Amazon CloudFront distribution. Set the S3 bucket as the origin. Use Amazon API Gateway and AWS Lambda functions for the backend APIs. Store the data in Amazon DynamoDB.
Xem đáp án
Đáp án đúng: D
S3 + CloudFront cho nội dung tĩnh, API Gateway + Lambda cho API, DynamoDB cho dữ liệu: tự co giãn tới hàng triệu request với độ trễ mili giây, không có máy chủ nào phải vận hành.
Câu hỏi thường gặp
CloudFront khác gì S3 static website?
S3 static website phục vụ file trực tiếp từ một region, chỉ HTTP với endpoint website. Đặt CloudFront phía trước giúp tải nhanh hơn nhờ cache gần người dùng, có HTTPS với tên miền riêng, và giữ bucket ở chế độ riêng tư bằng Origin Access Control.
CloudFront khác gì Global Accelerator?
CloudFront là CDN, cache nội dung HTTP ở các điểm gần người dùng. Global Accelerator không cache mà đưa traffic TCP/UDP vào mạng riêng của AWS qua hai IP tĩnh toàn cầu, hợp với game, VoIP hoặc ứng dụng cần failover giữa region nhanh.
CloudFront tính tiền thế nào?
Chủ yếu theo lượng dữ liệu truyền tới người dùng và số request, giá khác nhau theo khu vực. Truyền dữ liệu từ dịch vụ AWS (như S3) vào CloudFront không mất phí. Ngoài cách trả theo dùng, CloudFront còn có các gói giá cố định theo tháng.
Đề thi AWS hay hỏi gì về CloudFront?
Origin Access Control để chỉ CloudFront đọc được bucket S3, signed URL và signed cookie cho nội dung trả phí, TTL và invalidation, chặn theo quốc gia, gắn WAF, và dùng Lambda@Edge hay CloudFront Functions để chạy code ở edge.