Skip to Content

Edge functions

Running code at CloudFront edge locations with CloudFront Functions and Lambda@Edge, their triggers, limits and use cases, and how to choose.

What it is

Many modern applications run some logic at the edge, close to users. An edge function is custom code attached to a CloudFront distribution. It runs near users to cut latency.

CloudFront offers two kinds: CloudFront Functions and Lambda@Edge.

  • No servers to manage; deployed globally automatically.
  • Use case: customize the CDN content.
  • Billing is per use only. Fully serverless.

Uses include website security and privacy, dynamic web applications at the edge, search engine optimization (SEO), routing across origins and data centres, bot mitigation, real-time image transformation, A/B testing, user authentication and authorization, user prioritization, and user tracking and analytics.

Where they run

Where edge functions run: viewer request and viewer response happen between the viewer and CloudFront, origin request and origin response between CloudFront and the origin; CloudFront Functions run only on the viewer events, Lambda@Edge on all four Where edge functions run: viewer request and viewer response happen between the viewer and CloudFront, origin request and origin response between CloudFront and the origin; CloudFront Functions run only on the viewer events, Lambda@Edge on all four

Four points where code can change a request or response.
  • Viewer request: after CloudFront receives a request from a viewer.
  • Origin request: before CloudFront forwards the request to the origin (on a cache miss).
  • Origin response: after CloudFront receives the response from the origin.
  • Viewer response: before CloudFront sends the response to the viewer.

CloudFront Functions

CloudFront Functions are lightweight functions written in JavaScript.

  • For high-scale, latency-sensitive CDN customizations.
  • Sub-millisecond start-up and run time; millions of requests per second.
  • Change viewer requests and responses only.
  • A native feature of CloudFront: code is written, tested and published entirely in CloudFront.
  • Can read data from CloudFront KeyValueStore, a small global key-value store (for example a list of redirects), updated separately from the code.

Use them for:

  • Cache key normalization: transform request attributes (headers, cookies, query strings, URL) to make a better cache key;
  • Header manipulation: insert, modify or delete HTTP headers in the request or response;
  • URL rewrites or redirects;
  • Request authentication and authorization: create and check user-generated tokens (such as JWTs) to allow or deny requests.

Lambda@Edge

Lambda@Edge runs Lambda functions written in Node.js or Python.

  • Scales to thousands of requests per second (up to 10,000 per Region by default).
  • Can change all four events: viewer request, origin request, origin response, viewer response.
  • The function is written in one Region, us-east-1 (N. Virginia) and published as a numbered version, and CloudFront replicates it to its locations.

Use it for:

  • longer run times (several milliseconds or more);
  • adjustable CPU or memory;
  • third-party libraries, such as the AWS SDK to call other AWS services;
  • network access to external services;
  • file system access or access to the body of HTTP requests.

CloudFront Functions vs Lambda@Edge

  • Runtime: JavaScript vs Node.js and Python.
  • Scale: millions vs thousands of requests per second.
  • Triggers: viewer request and response only vs all four.
  • Run time: under a millisecond vs up to 30 seconds.
  • Memory: 2 MB vs 128 MB (viewer events) up to 10 GB (origin events).
  • Code size: 10 KB vs 50 MB.
  • Network and file system: very limited (one call to an origin of the same distribution) vs full access.
  • Request body: no vs yes.
  • Price: about one sixth of Lambda@Edge, with a free allowance vs charged per request and duration.

Updated since the slides

The slides give Lambda@Edge a maximum run time of 5 to 10 seconds and code size of 1 to 50 MB, and say CloudFront Functions have no network access. The AWS documentation now lists 30 seconds and 50 MB for all Lambda@Edge events, and allows CloudFront Functions one network call to an origin on the same distribution. The exam's rule of thumb is unchanged: quick header and URL changes → CloudFront Functions; heavy logic, network calls, request bodies → Lambda@Edge.

Real-world examples

  • A news site uses a CloudFront Function to redirect old URLs to new ones and to normalize query strings so more requests hit the cache.
  • A photo service uses Lambda@Edge on origin responses to resize images for each device, calling S3 with the AWS SDK.
  • A media company checks a signed JWT in a CloudFront Function on every viewer request, rejecting unauthorized users before they reach the origin.

Exam tips

  • "Lightweight, millions of requests per second, change headers, redirect URLs, normalize cache keys" → CloudFront Functions.
  • "Needs the AWS SDK", "network calls", "request body", "longer processing", "origin request or response" → Lambda@Edge.
  • "Authenticate users at the edge with a JWT" → CloudFront Functions (simple check) or Lambda@Edge (complex).
  • "Where do you create Lambda@Edge functions?" → us-east-1.

Common traps

  • "CloudFront Functions can run on origin requests." Viewer events only.
  • "Lambda@Edge functions can be written in any Region." They're created in us-east-1 and replicated.
  • "Use Lambda@Edge for simple header changes." CloudFront Functions are faster and much cheaper.

Check yourself

An edge function lab needs a CloudFront distribution and takes a while to deploy and delete; try these instead:

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt