- AWS Lambda@Edge (enabled Preview)
- One Amazon CloudFront Distribution (origin doesn't matter)
- IAM role (basic execution is enough)
- npm to install Node.js dependencies
The Lambda@Edge will be invoked whenever a new "Viewer Request" event is triggered by CloudFront.
The Lambda Function will behave as follows:
- If the requested resource is NOT available locally (i.e. not an HTML file), the request can proceed to the origin
- If the local template exists, it will be read and rendered using Plates with a few dynamic variables (i.e. "title" and "today)
- The resulting HTML is then minified and eventually compressed, based on the request HTTP headers (response headers are correctly set as well)
- The final HTTP body is directly returned to the client without hitting the CloudFront origin
cd this-gist
npm install
zip -r ../edge-deployment-package.zip ./*
- The deployment package cannot exceed 1MB, and a manual hack was required to include the 'html-minifier' library (i.e. reducing its size from 3MB to 500KB)
Have you checked to see if the built-in GZIP support (https://aws.amazon.com/blogs/aws/new-gzip-compression-support-for-amazon-cloudfront/) is still functional when running a Lambda@Edge function? If so, you might not need the GZIP library.