Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFront Lambda@Edge in NodeJS. Layer is not supported for cloudfront. How to use external libraries in edge lambda?

By using Lambda@Edge inside CloudFront I am not able to install packages thourgh npm as CloudFront does not allow layers:

According to AWS doc: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-edge-function-restrictions.html

Lambda Features

My approach is to validate jwt tokens using Viewer Request

Functions associated

There are multiple examples doing this, and they use common libraries such as:

import y from 'jsonwebtoken';
import z from 'jwk-to-pem';

But How should be referred if layers are not allowed? Using thus modules the application report errors:

Cannot find package 'jsonwebtoken' imported from /var/task/index.mjs"

On the other hand, creating a lambda with a cusotm layer with the following packages worked (But unable to use in aws-cloudfront):

{
  "dependencies": {
    "@types/jwk-to-pem": "^2.0.3",
    "jsonwebtoken": "^9.0.2",
    "jwk-to-pem": "^2.0.5",
    "lodash": "^4.17.21"
  }
}

Tried:

  • Create a layer on behalf of my lambda@Edge function.

Expected to happen:

  • Validate tokens in AWS@Lambda edge functions with external 3rd parties.

Actually result:

  • Layers are not allowed in AWS@Lambda
like image 633
ivi333 Avatar asked Jun 12 '26 09:06

ivi333


1 Answers

I was able to create the package without using a layer.

Solution: Deploy it as a zip file

Considerations: There is a maximum file size when using it in Lambda@Edge cloudfront.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html

like image 97
ivi333 Avatar answered Jun 16 '26 12:06

ivi333



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!