Running Logic at the Edge with CloudFront Functions and Lambda@Edge
This lesson compares CloudFront Functions and Lambda@Edge, explains how the triggering event affects cost and behaviour, and describes common edge patterns.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish CloudFront Functions and Lambda@Edge by events, capabilities and cost.
- Explain why expensive logic belongs on origin events.
- Choose an edge compute option for a latency requirement.
The two options
CloudFront Functions
CloudFront Functions run lightweight JavaScript in every edge location on viewer request and viewer response events, with sub-millisecond execution, very high scale and the lowest price. They suit URL rewrites and redirects, header manipulation, cache key normalisation, simple token validation and A/B routing, and they can read data from CloudFront KeyValueStore. They cannot make network calls or access the request body.
Lambda@Edge
Lambda@Edge runs Node.js or Python functions in Regional edge caches on viewer and origin request and origin response events. Functions can make network calls, read the body, run longer and use more memory, which suits authentication against an identity provider, dynamic origin selection, generating responses and image transformation. Functions are created in us-east-1 and replicated.
CloudFront Functions or Lambda@Edge?
Choosing the event
Viewer events run on every request, cached or not. Origin events run only when CloudFront goes to the origin, that is on cache misses. Expensive logic, such as image resizing, belongs on origin events with the results cached, while logic that must affect every request, such as redirects or security headers, belongs on viewer events, ideally in CloudFront Functions. Response headers policies add security and CORS headers without any code.
The following diagram shows where the four events occur and which code can run on each.

Common edge patterns
- Country or device redirects and URL rewrites on viewer request with CloudFront Functions.
- Redirect tables maintained by marketing in CloudFront KeyValueStore, without redeploying code.
- Token validation with CloudFront Functions, or full OIDC authentication with Lambda@Edge.
- Image resizing on origin request with Lambda@Edge, with resized images cached.
A global media site
A media site redirects visitors to their country's edition, lets the marketing team change thousands of vanity redirects daily, and resizes images for different devices.
A CloudFront Function on viewer request reads the country header and a KeyValueStore of redirects, which marketing updates through an API without deployments.
Image resizing runs in Lambda@Edge on origin request: the first request for a size generates and caches it, and later requests are served from cache, so the function runs only once per size and edge cache.
A Lambda@Edge function on viewer request resizes images and is expensive. What change cuts its cost?
Show answer
Move it to the origin request event so it runs only on cache misses, and cache the resized images, so each size is generated once per edge cache.
Exam Tips
Remember for the exam
- Simple rewrites, redirects or header changes on every request at lowest cost: CloudFront Functions.
- Logic that calls external services or changes the origin: Lambda@Edge.
- Run expensive logic only on cache misses: origin request events with caching.
- Change redirect data without redeploying: CloudFront KeyValueStore.