Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Cloudfront Edge Lambda for doing on the fly asset optimization

Notifications You must be signed in to change notification settings

mageops/aws-lambda-cloudfront-edge

Repository files navigation

Build Status

Do not use it

This Lambda is broken by design. Please do not use it. We will improve this concept one day.

Lambda@Edge for creativeshop

Requirements

Setting up

Use prebuilt deploy packages

If you don't need to modify the code you can use the prebuilt deploy packages provided as assets with each GitHub release.

They are built automatically using Travis CI *.

* Currently running tests on build is disabled because they do exact response body comparison which is different between Linux and MacOS.

Build lambda deploy package (optional)

Because our lambda requires native modules in npm packages e.g. for image optimization, compression etc. we need to rebuild entire node_modules on the same operating system that AWS Lambda is running on. Then the complete codebase is zipped into edge-lambda-deploy-package.zip archive which can be attached in Lambda Management Console.

To automatically generate edge-lambda-deploy-package.zip using docker image:

yarn create-package

Or enter the command directly:

docker run --rm --tty --volume "$(pwd):/var/app" mageops/aws-lambda-build:nodejs10.x nodejs-yarn edge-lambda-deploy-package

NOTE: Even though lambda's runtime should be based on Amazon Linux 2 which has libjpeg-turbo included by default, this library seems to be missing. The custom build hook installs appropriate so files into the lib/ subdirectory. It's possible that Lambda@Edge still uses Amazon Linux 1 as the system runtime, what is not surprising given that this specific lambda type does not support node12.x in contrast to "standard ones".

Docker image for building lambdas

The package is built using mageops/aws-lambda-build. Check the corresponding GitHub repository for more information.

Configuring Lambda function

Go to Lambda Management Console and create new function (make sure you have US East (N. Virginia) region selected) with following data:

Name: originRequest

Runtime: Node.js 10.x

Role: Create new role from template(s)

Role name: Your desired name for a role.

Policy templates: Basic Edge Lambda permissions

Note: You can create your own role if you know what you are doing.

You will be redirected to function configuration, there you need to setup the following:

Function Code

  1. Select Upload a .ZIP file in Code entry type field.
  2. Upload previously generated edge-lambda-deploy-package.zip.
  3. Make sure Runtime is set to Node.js 8.10.
  4. Set Handler field to origin-request.handler.

Basic Settings

  1. Set Memory (MB) limit to 1024MB for best performance/cost ratio.
  2. Set Timeout to about 20s (compressing images larger then few MB takes a lot of time and can still be slower).

Attaching to CloudFront

  1. Save all the changes.
  2. Release new version using Actions dropdown.
  3. You will be redirected to certain version configuration.
  4. In Designer tab select CloudFront in Add triggers section.
  5. Scroll down to Configure Triggers section.
  6. Select your target CloudFront distribution in the Distribution field.
  7. Leave cache behaviour at *.
  8. Set CloudFront event to Origin Request.
  9. Check Enable trigger and replicate.
  10. Confirm by clicking the Add button.
  11. Hit Save button at the top-right.
  12. Wait for CloudFront to apply the changes(usually a minute or two).

About

Cloudfront Edge Lambda for doing on the fly asset optimization

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages