What is Lambda@Edge and what are its use cases?

Answer

Lambda@Edge is an extension of AWS Lambda that lets you run functions at CloudFront edge locations (200+ locations worldwide) in response to CloudFront events, closer to end users. It intercepts four types of CloudFront events: viewer request (before checking cache), origin request (on cache miss, before forwarding to origin), origin response (after origin response, before caching), viewer response (before sending to client). Use cases: (1) A/B testing — redirect percentage of traffic to different URLs at edge; (2) URL rewrites/redirects — rewrite URLs based on device type, geolocation, or query params; (3) Authentication at edge — validate JWT tokens at edge before hitting origin; (4) Security headers — add CSP, HSTS headers to all responses; (5) Dynamic content — personalize cached pages based on cookies; (6) Image transformation — resize/convert images at edge. Limitations: 5-second timeout, 128MB memory (much more restrictive than standard Lambda). CloudFront Functions is a lighter alternative for simple URL manipulations.