AWS Cloud Architecture•Part 4: Storage, Content Delivery & Databases (36 - 50)
CloudFront Functions vs Lambda@Edge.
In Simple Words:
- CloudFront Functions: Lightweight JavaScript running at 450+ Edge Locations in sub-milliseconds. No network access. Ideal for URL rewrites, redirecting, and header manipulation.
- Lambda@Edge: Full Node.js/Python running at Regional Edge Caches. Allows network calls, third-party API lookups, and server-side rendering at the edge.
javascript
// CloudFront Function: URL rewrite appending index.html for SPAs:
function handler(event) {
var request = event.request;
var uri = request.uri;
if (uri.endsWith('/')) {
request.uri += 'index.html';
} else if (!uri.includes('.')) {
request.uri = '/index.html';
}
return request;
}