Lambda@Edge and CloudFront Functions

Here's a comprehensive comparison between Lambda@Edge and CloudFront Functions:

  1. Runtime Environment:

  • Lambda@Edge: Node.js and Python

  • CloudFront Functions: JavaScript only (ECMAScript 5.1)

  1. Execution Context:

  • Lambda@Edge: Can run at four stages:

    • Viewer Request

    • Origin Request

    • Origin Response

    • Viewer Response

  • CloudFront Functions: Only two stages:

    • Viewer Request

    • Viewer Response

  1. Performance & Scale:

  • Lambda@Edge:

    • Startup latency: ~1-2ms

    • Maximum runtime: 5-30 seconds

    • Memory: 128MB to 10GB

  • CloudFront Functions:

    • Startup latency: sub-millisecond

    • Maximum runtime: < 1ms

    • Memory: 2MB

  1. Use Cases: Lambda@Edge:

  • Complex processing

  • Third-party API calls

  • Large-scale manipulations

  • Access to AWS services

  • Cookie manipulation

  • Complex redirects

  • Authentication/Authorization

CloudFront Functions:

  • URL rewrites/redirects

  • Header manipulation

  • Simple A/B testing

  • Request normalization

  • Simple authentication

  • Cache key normalization

  • Request validation

  1. Pricing:

  • Lambda@Edge: Pay for compute time and requests

  • CloudFront Functions: Significantly cheaper, pay per request only

  1. Development & Deployment: Lambda@Edge:

exports.handler = async (event) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    
    // Complex processing here
    const response = await fetch('https://api.example.com/data');
    
    return request;
};

CloudFront Functions:

function handler(event) {
    var request = event.request;
    var headers = request.headers;
    
    // Simple header manipulation
    headers['x-custom-header'] = {value: 'some-value'};
    
    return request;
}
  1. Key Limitations: Lambda@Edge:

  • Regional deployment required

  • Longer cold starts

  • Higher latency for complex operations

CloudFront Functions:

  • No external network calls

  • Limited runtime

  • No filesystem access

  • No access to additional AWS services

Choose CloudFront Functions when:

  • You need simple, lightweight processing

  • Sub-millisecond performance is crucial

  • Cost optimization is important

  • Processing is limited to viewer request/response

Choose Lambda@Edge when:

  • You need complex processing

  • External API calls are required

  • You need access to AWS services

  • Processing at origin request/response is needed

Last updated

Was this helpful?