Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

alphagov/aws-lambda-at-edge-examples

Repository files navigation

aws-lambda-at-edge-examples

Terraform and example code for using Lambda@Edge with CloudFront to alter requests and responses

Examples

What is Lambda@Edge?

Lambda@Edge are distributed Lambdas that are for CloudFront distributions to utilise, they run on AWS edge infrastructure and not in one particular region.

There are currently only two supported languages: Node.js (version 12) and Python (3.7).

The Lambdas should be quick due to them potentially handling lots of requests.

This repo holds examples that you can use and/or adapt for your CloudFront distributions.

What are triggers?

There are four Lambda@Edge triggers, that occur in this order:

  1. Viewer Request
  2. Origin Request
  3. Origin Response
  4. View Response

(nice diagram on the different triggers from AWS here)

What trigger do I need?

Here are some examples:

  • I want to change the origin dynamically for every request: Viewer Request
  • I want to change the origin dynamically but then cache: Origin Request
  • I want to change the origin response headers an then cache: Origin Response
  • I want to change the response headers on every request: Viewer Response
  • I want to check a request header on every request (like Authorization): Viewer Request

This AWS link provides more details on choosing a trigger: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-how-to-choose-event.html

Are there different limits for Lambda@Edge?

Yes!

Viewer requests and responses need to be the quickest (as they trigger on every request), so their limits are:

  • 128MB memory
  • 5 second timeout
  • 40 KB response (headers and body)
  • 1 MB size of Lambda function

Origin request and responses can be slightly slower and bigger (they only trigger if the cache is empty for a given request):

  • memory limit is same as regular Lambdas
  • 30 second timeout
  • 1 MB response (headers and body)
  • 50 MB size of Lambda function

See this AWS link for further details: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html

About

Terraform and example code for using Lambda@Edge with CloudFront to alter requests and responses

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published