Lesson 10 of 10 Serverless and API Architecture

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.

Sort into groups

CloudFront Functions or Lambda@Edge?

Exam skills 1.1.11

Choose the edge compute option for each task.

Log in to try this activity Interactive activities are free with an account.

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.

CloudFront Functions
EventsViewer request and response
Network access and bodyNo
Typical latency and costSub-millisecond, lowest cost
Choose when Simple, high-volume request and response changes.
Lambda@Edge
EventsViewer and origin request and response
Network access and bodyYes
Typical latency and costMilliseconds, higher cost
Choose when Logic that calls services, reads the body or changes the origin.

The following diagram shows where the four events occur and which code can run on each.

The four CloudFront events. Viewer request and viewer response run on every request, cached or not, and can use CloudFront Functions or Lambda@Edge. Origin request and origin response run only when CloudFront goes to the origin on a cache miss, and use Lambda@Edge, which suits expensive work whose result is then cached.
Origin events run only on cache misses, so work placed there runs once per cached object rather than once per request.

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.
Worked example

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.

Check yourself

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.