Terraform and example code for using Lambda@Edge with CloudFront to alter requests and responses
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.
There are four Lambda@Edge triggers, that occur in this order:
- Viewer Request
- Origin Request
- Origin Response
- View Response
(nice diagram on the different triggers from AWS here)
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
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