Ace Cloud Interviews
Home/AWS Tutorial/CloudFront
🌐

AWS Networking & CDN

CloudFront

Global CDN delivering content with low latency from 450+ edge locations worldwide

Amazon CloudFront is a global content delivery network (CDN) that distributes content from 450+ edge locations worldwide, reducing latency by serving requests from the location closest to the user. It integrates natively with S3, ALB, API Gateway, and custom origins, and is AWS's primary solution for accelerating both static and dynamic content delivery.

How CloudFront Delivers Content

When a user requests content, CloudFront routes the request to the nearest edge location (Point of Presence). If the edge has a cached copy (cache hit), it returns it immediately. On a cache miss, CloudFront fetches from the origin, caches the response according to cache behavior rules, then returns it.

ComponentRoleKey Setting
DistributionTop-level CloudFront resource with a .cloudfront.net domainAlternate domain names (CNAMEs) require an ACM certificate
OriginWhere CloudFront fetches content on cache missCan be S3, ALB, EC2, API GW, or any HTTP server
Cache BehaviorPath-pattern rules controlling caching and routingOne default (*) plus any number of path-specific behaviors
Origin Request PolicyWhat headers/cookies/query strings to forward to originAvoid forwarding all headers - destroys caching efficiency
Cache PolicyWhat determines the cache key and TTLSeparate from forwarding - key only what varies the response
Edge LocationCache node serving end users450+ globally; content cached here after first request
Regional Edge CacheLarger cache tier between edge and originReduces origin load; longer TTL than edge locations
💡

CloudFront distributions require ACM certificates in us-east-1 (N. Virginia) regardless of where your application runs. This is a common gotcha - a certificate in ap-southeast-1 will not work for CloudFront even if your origin is there.

Cache Behaviors and Cache Key Design

Cache behaviors define how CloudFront handles requests matching a path pattern. The default behavior (*) matches everything not matched by more specific patterns. More specific patterns take precedence.

ScenarioCache Behavior ConfigWhy
Static assets (/static/*)TTL: 1 year, compress: yes, no cookies forwardedContent is versioned by filename hash; aggressive caching is safe
API responses (/api/*)TTL: 0 (no cache) or short TTL, forward auth headerDynamic data; must validate freshness frequently
HTML pages (/)TTL: 5-60 min, forward Accept-EncodingBalance freshness vs origin load
User uploads (/uploads/*)Signed URLs required, restrict S3 origin accessPrivate content should not be publicly cacheable

Cache key design directly impacts hit ratio. Only include values in the cache key that actually change the response. Forwarding all cookies or all headers fragments the cache and effectively disables caching.

bash
# Invalidate specific paths after a deployment
aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE \
  --paths "/index.html" "/static/js/*"

# Invalidate everything (costs $0.005 per path after first 1000/month)
aws cloudfront create-invalidation \
  --distribution-id E1EXAMPLE \
  --paths "/*"
⚠️

Invalidations cost money after the first 1000 paths per month ($0.005/path). Prefer versioned filenames (app.abc123.js) over invalidations for static assets. Reserve invalidations for emergency cache busting of HTML or config files.

Security Features: OAC, WAF, Signed URLs

CloudFront provides several security layers to protect your origin and control access to content.

FeaturePurposeHow It Works
Origin Access Control (OAC)Prevent direct S3 access, force CloudFrontCloudFront signs requests to S3 with SigV4; S3 bucket policy allows only the OAC
Signed URLsTime-limited access to specific objectsURL includes expiry + signature; used for private downloads
Signed CookiesTime-limited access to multiple objectsCookie with policy + signature; used for streaming/private sections
AWS WAFBlock malicious requests at the edgeAttach a WAF Web ACL to block IPs, SQL injection, XSS, rate limits
Geo RestrictionBlock or allow by countryUses IP geolocation; not 100% accurate for VPNs
HTTPS OnlyEnforce TLS between user and CloudFrontViewer protocol policy: Redirect HTTP to HTTPS
Field-Level EncryptionEncrypt sensitive form fields at the edgeCloudFront encrypts specific POST fields with your public key
💡

Always use Origin Access Control (OAC) instead of the older Origin Access Identity (OAI) for S3 origins. OAC supports all S3 regions, SSE-KMS encrypted buckets, and is the current AWS recommendation.

Lambda@Edge vs CloudFront Functions

Both run code at the edge, but they target different use cases and have very different performance and cost profiles.

FeatureCloudFront FunctionsLambda@Edge
RuntimeJavaScript (ES5)Node.js or Python
Max execution time1ms5s (viewer) / 30s (origin)
Memory2 MB128 MB - 10 GB
TriggersViewer request/response onlyViewer and origin request/response
Access to request bodyNoYes (origin request/response)
Cost$0.10 per 1M invocations$0.60 per 1M invocations + duration
LatencySub-millisecondMilliseconds
Use casesURL rewrites, header manipulation, simple redirectsAuth, A/B testing, dynamic content generation, image resizing
💡

Use CloudFront Functions for lightweight viewer-side manipulations (URL normalization, security headers). Use Lambda@Edge when you need more compute, access to the network, or need to modify origin requests.

CloudFront Pricing and Cost Optimization

CloudFront pricing varies by region and traffic tier. Data transfer out (to internet) is the main cost driver, plus HTTP request counts.

Cost ComponentApproximate PriceOptimization
Data transfer out (US/EU)$0.0085/GB (first 10 TB)Compress responses; use cache TTLs aggressively
Data transfer out (Asia)$0.140/GBConsider price class to exclude expensive regions
HTTP requests$0.0075 per 10,000Consolidate small files; use HTTP/2 multiplexing
HTTPS requests$0.0100 per 10,000Expected for security; unavoidable
Invalidations$0.005 per path after 1,000Use versioned file names instead
Lambda@Edge$0.60 per 1M + durationUse CF Functions for simple transformations

Price Class lets you restrict distribution to edge locations in specific regions. Price Class 100 (US/EU/Canada) is cheapest. Use it for services whose users are predominantly in those regions.

🎯

Interview Focus Points

  • 1How does CloudFront cache behavior path pattern matching work? Which patterns take priority?
  • 2Explain Origin Access Control and why you would use it over a public S3 bucket.
  • 3A user reports stale content after a deployment. What are your options to fix it immediately and avoid it in the future?
  • 4When would you use Lambda@Edge vs CloudFront Functions? Give a concrete example of each.
  • 5How would you implement authentication at the CloudFront edge for a private SPA?
  • 6Explain the difference between viewer protocol policy and origin protocol policy.
  • 7How does CloudFront handle origin failover? What is an origin group?
  • 8What is the difference between Signed URLs and Signed Cookies? When do you use each?
  • 9How would you set up CloudFront for a Next.js static export with dynamic routes?
  • 10Explain how cache key design impacts CloudFront hit ratio and origin load.