Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to bundle 'cognito-at-edge' with Lambda@Edge function? #38

Open
aiden-sobey opened this issue Jun 20, 2022 · 3 comments
Open

How to bundle 'cognito-at-edge' with Lambda@Edge function? #38

aiden-sobey opened this issue Jun 20, 2022 · 3 comments

Comments

@aiden-sobey
Copy link

Hi, I am using Typescript CDK to deploy a Lambda@Edge function that uses cognito-at-edge.
When the Lambda runs, it returns a 503 error with: Error: Cannot find module 'cognito-at-edge'

I followed the instructions in the README exactly:

const { Authenticator } = require('cognito-at-edge');

const authenticator = new Authenticator({
        ... my details ...
});

exports.handler = async (request) => authenticator.handle(request);

How is this package meant to be bundled with Lambda@Edge? I can't find any instructions in this README, or elsewhere.

@aiden-sobey
Copy link
Author

There is very little documentation around this, so putting it out there to help others-
Using CDK you can deploy any Lambda to us-east-1 then reference it as a Lambda@Edge. As long as your lambda does not rely on any functionality not supported by edge lambdas it should work fine. I defined my lamdba as so:

import * as nambda from "aws-cdk-lib/aws-lambda-nodejs";
import * as lambda from "aws-cdk-lib/aws-lambda";

    this.lambda = new nambda.NodejsFunction(this, "auth", {
      handler: "handler",
      entry: path.join(__dirname, "../src/edge/auth.js"),
      runtime: lambda.Runtime.NODEJS_16_X,
      timeout: Duration.seconds(5),
      logRetention: RetentionDays.ONE_WEEK,
      bundling: {
        nodeModules: ["cognito-at-edge"],
      },
    });

This bundles the cognito-at-edge NPM package with my lambda, and this works at Edge which is awesome.
The code for my lambda is exactly as it is in my original post, and I reference it during creation of my CloudFront distribution:

this.distribution = new cf.Distribution(this, "server", {
    ...
    defaultBehavior: {
        edgeLambdas: [
            {
                eventType: cf.LambdaEdgeEventType.VIEWER_REQUEST,
                functionVersion: this.lambda.currentVersion,
            }
        ]
    }

I've also found a way to package props into the edge lambda at deploy time which is great, acts as a replacement for environment variables which Lambda@Edge does not have.

@karthikvadla
Copy link

Can you please explain more about packing props during deploy time? Are you using ssm?

@unitypark
Copy link

@aiden-sobey
hi! I have created a demo web app using this library for my edge function for cloudfront viewer request.

for bundling, in edge folder run "npm run webpack"

full code could be found at below link! 🚀
https://github.com/unitypark/aws-serverless-demos/tree/main/serverless-web-hosting/cloudfront-http-api-cognito

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants