AWS - Cloudfront
CloudFront & Edge Functions
- Edge Functions are code that you write and attach to Cloudfront Distributions
- Runs close to your users to minimize latency
- Cloudfront provides 2 types of Edge Functions - CloudFront Functions & Lamdba@Edge
- Allows us to customize the CDN content
General Use Cases - CloudFront Functions & Lamdba@Edge
- Dynamic Web Application at the Edge
- Search Engine Optimization (SEO)
- Intelligently Route Acoess Origins and Data Centers
- Bot Mitigation at the Edge
- Real-time Image Transformation
- A/B Testing
- User Authentication & Authorization
- User Prioritization
- User Tracking and Analytics
Cloudfront Functions
- Lightweight Functions written in Javascript (only support Javascript)
- For high-scale, latency-sensitive CDN customizations
- Sub-ms startup times, millions of requests/second
- Is a native feature of CloudFront
- Can manage code entirely within CloudFront
- Used to change Viewer requests and response:
- Viewer Request: after CloudFront receives a request from a Viewer
- Viewer Response: before CloudFront forwards the response to a Viewer
- Free tier available
- 1/6 the price of Lambda Edge
- No Network Access or File System Access
- No access to request body
- Total package size of 10 KB
- Max memory of 2 MB
- Max execution time < 1ms
- Main use cases:
- Cache key normalization
- HTTP Header manipulation
- URL rewrites or redirect
- Request Authentication & Authorization
Lamdba@Edge
- are Lambda functions written in NodeJs & Python
- scales to thousands of request/second
- used to change CloudFront requests and responses:
- Viewer Request: after CloudFront receives a request from a Viewer
- Origin Request: before CloudFront forwards the request to the Origin
- Origin Response: after CloudFront receives a response from a Origin
- Viewer Response: before CloudFront forwards the response to a Viewer
- author your functions in one AWS regions (us-east-1), then CloudFront replicates to its locations
- Has Network Access and File System Access
- Has access to request body
- No Free tier, charged per request & duration
- Total package size of 1 MB - 50 MB
- Max memory of 128 MB, up to 10 GB
- Max execution time of 5 - 10 seconds
Use case - To resolve NextJs static export routing