Skip to main content

🌍 03. AWS CloudFront

AWS CloudFront is a Content Delivery Network (CDN) that improves content delivery performance by caching content at edge locations globally.

⚑ Key Features​

  • Faster Content Delivery: Cached at 216+ global edge locations
  • Low Latency: Users are served from the nearest edge location
  • DDoS Protection: Integrated with AWS Shield and AWS WAF
  • Security: Uses Origin Access Control (OAC) for secure S3/backend access
  • Global Reach: Improves user experience worldwide

☁️ CloudFront Origins​

An Origin is the source of your content β€” the location where CloudFront fetches your files (HTML, images, videos, etc.).

When a user requests content:

  • If cached β†’ served instantly from the nearest edge
  • If not cached β†’ fetched from origin, then cached for future use

πŸͺ£ A. Amazon S3 Bucket (Static Origin)​

Used for static files like images, CSS, JS, and HTML.

Key Points:

  • CloudFront fetches and caches data from S3.
  • Use OAC/OAI to restrict access (only CloudFront can read).
  • Can be configured as an ingress path to upload via edge β†’ S3.

πŸ—οΈ B. VPC Origin (Private Applications)​

For applications hosted in VPC private subnets

Examples:

  • Application Load Balancer (ALB)
  • Network Load Balancer (NLB)
  • EC2 Instances

Security:

  • Connects privately via AWS PrivateLink or VPC endpoints
  • Requests pass through CloudFront (WAF, Shield) before backend access

🌐 C. Custom HTTP Origin​

Used for any HTTP/HTTPS server, inside or outside AWS.

Examples:

  • S3 bucket with Static Website Hosting enabled
  • On-premises servers
  • Other cloud providers

Notes:

  • Must support HTTP/HTTPS
  • Ideal for hybrid or legacy systems
  • Can still apply WAF and Shield protections

IAM Roles Example

CloudFront can connect to S3, private VPCs, or any HTTP backend β€” maintaining high performance, global caching, and strong security.


βš–οΈ CloudFront vs S3 Cross-Region Replication (CRR)​

FeatureCloudFrontS3 Cross-Region Replication (CRR)
PurposeCDN for fast content deliveryReplicates data between regions
NetworkAWS Global Edge NetworkRequires manual region setup
Data BehaviorCached temporarilyCopied permanently
Update FrequencyBased on TTL (cache duration)Near real-time replication
AccessRead-only cached copiesFull replicated copies
Best ForStatic content (images, JS, CSS)Dynamic or synced regional data
ExampleGlobal website deliveryData backup / multi-region sync

🧠 In short:

  • CloudFront = Global caching β†’ fast reads
  • S3 CRR = Data replication β†’ regional consistency

πŸš€ CloudFront + S3 Demo​

In this demo, we configure CloudFront to serve files securely from a private S3 bucket using Origin Access Control (OAC).

🧩 Step 1: Create CloudFront Distribution​

  • Open CloudFront β†’ Create Distribution
  • Select your S3 bucket as origin (e.g., demo-cloudfront-himanshu-v4)
  • Keep Origin Path index.html (files stored in root)

πŸ” Step 2: Enable Origin Access Control (OAC)​

  • Choose Allow, use OAC (Allow private S3 bucket access to CloudFront)
IAM Roles Example

βš™οΈ Step 3: Configure Settings​

  • Keep default cache behavior and settings
  • Disable optional security features (for demo/cost control)
IAM Roles Example

πŸ—οΈ Step 4: Deploy Distribution​

  • Click Create Distribution
  • Wait until status = β€œDeployed”
  • CloudFront is now active globally

πŸ” Step 5: Verify S3 Bucket Policy​

  • Open S3 β†’ Permissions β†’ Bucket Policy
  • Confirm CloudFront automatically added permissions
    (only your CloudFront distribution can read from S3)
IAM Roles Example

🌐 Step 6: Test CloudFront Domain​

  • Copy CloudFront domain (e.g., dxxxx.cloudfront.net)
  • Test the URLs:
    • /coffee.jpg
    • /beach.jpg
    • /index.html

βœ… Files load successfully β€” served via CloudFront, not directly from S3.

πŸ“ˆ What Happened​


  • CloudFront created a secure connection to S3 via OAC
  • S3 bucket policy was updated to block public access
  • Requests flow:
    1. User β†’ CloudFront Edge
    2. CloudFront β†’ S3 (private access)
    3. File cached β†’ Served globally
  • Next requests load instantly from the nearest edge location

🏁 Result​

βœ… S3 bucket stays private
βœ… CloudFront serves files securely and globally
βœ… Caching boosts performance and reduces cost
βœ… Setup is automated and easy to manage