** this is not a SST issue but CDK within SST I’m...
# help
d
** this is not a SST issue but CDK within SST I’m a bit in over my head and looking for some help. My goal is to have a function run at the edge, every time a resource is called from my static site. The function is located with the rest of the my functions (in
src/
. my project is in
us-east-1
. Below is a simplified version of my code.
Copy code
import * as sst from "@serverless-stack/resources";

import { LambdaEdgeEventType, experimental } from "aws-cdk-lib/aws-cloudfront";
import * as lambda from "aws-cdk-lib/aws-lambda";

export default class MarketingStack extends sst.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const originRewriteHandler = new experimental.EdgeFunction(
      this,
      "OriginRewriteFunc",
      {
        runtime: lambda.Runtime.NODEJS_14_X,
        handler: "src/rewriter.main",
      }
    );

    const staticSite = new sst.StaticSite(this, "Site", {
      path: "website",
      errorPage: "404.html",
      buildOutput: "public",
      buildCommand: "npm run build",
    });

    staticSite.cfDistribution.addBehavior("/*", staticSite.s3Bucket, {
      edgeLambdas: [
        {
          functionVersion: originRewriteHandler.currentVersion,
          eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
        },
      ],
    });
  }
}
When I try to tun this I get a type error that it can’t read
bind of undefined
. Which makes sense since I have no idea what I’m doing 🤣 . Actually, I figure that it’s trying to connect the bucket and the lambda but I’m missing something. I don’t really kno how to debug this.
f
Hey @Devin, do you have the full error message from terminal?
d
Thanks @Frank. It’s got something to do with how I’m setting up the edge function but it’s a little unclear why this is happening. I’m actually going to try to solve this some more today
Copy code
TypeError: Cannot read property 'bind' of undefined
    at new Function (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/node_modules/aws-cdk-lib/aws-lambda/lib/function.ts:350:29)
    at EdgeFunction.createInRegionFunction (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/node_modules/aws-cdk-lib/aws-cloudfront/lib/experimental/edge-function.ts:122:26)
    at new EdgeFunction (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/node_modules/aws-cdk-lib/aws-cloudfront/lib/experimental/edge-function.ts:44:14)
    at new MarketingStack (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/stacks/MarketingSiteStack.js:27:34)
    at Object.main (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/stacks/index.js:12:3)
    at Object.<anonymous> (/Users/devinfitzsimons/Backup/serverless-stack/comics-helper/.build/run.js:94:16)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
I think what I need is the
code From AssetPath
portion
f
Ah yeah, u’d need to pass in
code
to
EdgeFunction
d
yeah I just don’t really understand where code comes from
does that makes sense @Frank
f
Take the example in the link above:
Copy code
const myFunc = new cloudfront.experimental.EdgeFunction(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});
You want to point the code path to the directory where the Lambda handler is. In this case, it’s pointing to the “lambda-handler” directory relative to where the stack file is.