Course contentsShow
← Cloud and Infrastructure
Lesson 615 of 2,98911. CDN and Edge ComputingFree lesson

CloudFront Functions vs Lambda@Edge

Comparing CloudFront Functions and Lambda@Edge: execution limits, event types, use cases, and pricing considerations.

CloudFront Functions vs Lambda@Edge

You'll learn: How to choose between CloudFront Functions and Lambda@Edge by understanding their execution limits, supported events, ideal use cases, and cost differences.

Core Differences

Both CloudFront Functions and Lambda@Edge run code at AWS edge locations, but they're built for different scenarios—think of CloudFront Functions as a lightweight sprint runner and Lambda@Edge as a versatile marathon runner.

Execution Limits and Performance

CloudFront Functions execute in under 1 millisecond with strict limits: maximum 2MB memory, 10KB code size, and sub-millisecond runtime. They're written in JavaScript only.

Lambda@Edge allows up to 30 seconds execution time (viewer events) or 5 seconds (origin events), supports up to 10GB memory, 50MB code packages, and runs Node.js or Python. It's significantly more powerful but slower to start.

Supported Event Types

CloudFront Functions only trigger on:

  • Viewer Request (before cache lookup)
  • Viewer Response (before returning to user)

Lambda@Edge supports all four CloudFront events:

  • Viewer Request and Response
  • Origin Request (cache miss, before origin)
  • Origin Response (after origin responds)

Use Cases Comparison

Use CloudFront Functions for:

  • Header manipulation (adding security headers)
  • URL rewrites and redirects
  • Simple request validation
  • Cache key normalization

Use Lambda@Edge for:

  • External API calls or database lookups
  • Image transformation and optimization
  • Complex authentication logic
  • A/B testing with external configuration

Pricing Considerations

CloudFront Functions cost ~$0.10 per million invocations—dramatically cheaper.

Lambda@Edge charges per request (0.60permillion)∗∗plus∗∗computetime(0.60 per million) **plus** compute time (0.00005001 per GB-second), making it 10-100x more expensive depending on duration.

Key Takeaway: Choose CloudFront Functions for simple, ultra-fast transformations at the edge; use Lambda@Edge when you need more execution time, external resources, or origin event access—but expect higher costs.