awslabs / cognito-at-edge

Serverless authentication solution to protect your website or Amplify application
Apache License 2.0
168 stars 54 forks source link

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

Open aiden-sobey opened 2 years ago

aiden-sobey commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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

unitypark commented 1 year ago

@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