CloudFront — CDN & Edge
Serve content from 450+ edge locations worldwide — cache, compress, protect, and deliver at the speed of light
Skills taught: aws.cloudfront, aws.cdn, aws.edge-locations, aws.cache-behaviors, aws.cloudfront-functions, aws.https
Why CDNs exist
Physics sets a hard limit on response time: data can travel no faster than the speed of light. Mumbai to New York is ~140ms at the speed of light; realistic internet latency adds another 50-100ms. If your server is in us-east-1 and a user is in Mumbai, every request has a 200-300ms baseline latency — before your application does anything. A CDN solves this by caching your content at edge locations near users: Mumbai user hits a Mumbai edge location, response in 5ms.
CloudFront distribution — setup
# Most teams use the console for CloudFront (complex config)
# Here's the CLI approach for a simple S3 + HTTPS setup:
# Create distribution pointing to S3 bucket website endpoint
aws cloudfront create-distribution --distribution-config '{
"Comment": "My App CDN",
"DefaultRootObject": "index.html",
"Origins": {
"Quantity": 1,
"Items": [{
"Id": "S3-myapp",
"DomainName": "myapp-bucket.s3-website-ap-south-1.amazonaws.com",
"CustomOriginConfig": {
"HTTPPort": 80,
"OriginProtocolPolicy": "http-only"
}
}]
},
"DefaultCacheBehavior": {
"TargetOriginId": "S3-myapp",
"ViewerProtocolPolicy": "redirect-to-https",
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"Compress": true
},
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
},
"Enabled": true,
"PriceClass": "PriceClass_All"
}'
# Result: CloudFront domain like abc123.cloudfront.net
# Point your custom domain's CNAME to this
# Invalidate cache after deploying new files
aws cloudfront create-invalidation \
--distribution-id EXXXXXXXXXX \
--paths '/*' # invalidate all cached files
# or specific: '/index.html' '/static/*'
Cache behaviors — the power of CloudFront
A CloudFront distribution can have multiple 'behaviors' based on URL path:
PATH PATTERN TTL BEHAVIOR
/static/* 31536000s Long cache (1 year) — hashed filenames
/*.js 31536000s JS bundles with content hash in name
/*.css 31536000s CSS with content hash in name
/api/* 0s Never cache (proxy to API server)
/index.html 0s Never cache (always fresh)
/* (default) 86400s Cache 1 day
WHY DIFFERENT TTLs:
Static assets (app.abc123.js) — filename includes content hash
If content changes, filename changes → automatic cache bust
So you can safely cache forever
HTML files — reference the JS/CSS files by name
Must be fresh so users get the latest js/css filenames
Cache for 0 seconds (or very short)
API responses — dynamic, must never be cached (unless you explicitly
want edge caching for specific endpoints like public pricing pages)
HTTPS with CloudFront + ACM
# ACM (Certificate Manager) provides free SSL/TLS certificates
# For CloudFront, the certificate MUST be in us-east-1 (global service)
# Request a certificate
aws acm request-certificate \
--domain-name myapp.com \
--subject-alternative-names www.myapp.com \
--validation-method DNS \
--region us-east-1 # required for CloudFront
# ACM returns a CNAME record to add to your DNS
# Once validated, the certificate auto-renews forever
# In CloudFront distribution config:
# ViewerCertificate:
# AcmCertificateArn: arn:aws:acm:us-east-1:xxx:certificate/xxx
# SslSupportMethod: sni-only
# MinimumProtocolVersion: TLSv1.2_2021
# ViewerProtocolPolicy: redirect-to-https
# → All HTTP requests are 301 redirected to HTTPS automatically
Tip: Always use CloudFront in front of your S3 website, not S3 directly. S3 website hosting doesn't support HTTPS for custom domains (only HTTP). CloudFront + ACM gives you free HTTPS with modern TLS. It also protects your S3 bucket from direct access (use OAC — Origin Access Control — to restrict S3 to only CloudFront). This prevents people from accessing the S3 URL directly and bypassing CloudFront.
Frequently Asked Questions
How much does CloudFront cost?
CloudFront's pricing: $0.0085 per GB data transfer (first 10TB), $0.0075 per 10,000 HTTPS requests. For a typical small website serving 100GB/month, that's $0.85/month in transfer + ~$0.075 in requests = under $1/month. For most apps, CloudFront + S3 is cheaper than any other hosting option and faster for global users.