Heya! I'm trying to attach a Lambda to the Viewer...
# help
t
Heya! I'm trying to attach a Lambda to the Viewer Request event using StaticSite... I'm probably overlooking something silly. Here's the error...
Copy code
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions.
Here's my setup...
Copy code
const viewerRequest = new Function(this, "viewer-request", {
      handler: "stack/cloudfront/viewer-request.handler",
      memorySize: 128,
      timeout: 5,
      // permissions: [
      //   new PolicyStatement({
      //     effect: Effect.ALLOW,
      //     actions: ["lambda:InvokeFunction"],
      //     resources: [viewerRequestHandler.functionArn],
      //   }),
      // ],
    });
    viewerRequest.node.host.environment = {};
    viewerRequest.node.host.role.assumeRolePolicy.statements = [
      new PolicyStatement({
        effect: Effect.ALLOW,
        actions: ["sts:AssumeRole"],
        principals: [
          new ServicePrincipal("<http://lambda.amazonaws.com|lambda.amazonaws.com>"),
          new ServicePrincipal("<http://edgelambda.amazonaws.com|edgelambda.amazonaws.com>"),
        ],
      })
    ];

    const site = new StaticSite(this, "website", {
      path: "stack/website",
      buildOutput: "dist",
      buildCommand: "yarn build",
      environment: { API_ENDPOINT: api.url },
      disablePlaceholder: true,
      cfDistribution: {
        priceClass: PriceClass.PRICE_CLASS_100,
        defaultBehavior: {
          allowedMethods: ["GET"],
          edgeLambdas: [{
            eventType: LambdaEdgeEventType.VIEWER_REQUEST,
            functionVersion: viewerRequest.currentVersion,
          }],
        },
        // additionalBehaviors: {
        //   "/files": {
        //     origin: new S3Origin(bucket.s3Bucket),
        //     allowedMethods: ["GET"],
        //     edgeLambdas: [{
        //       eventType: LambdaEdgeEventType.VIEWER_REQUEST,
        //       functionVersion: viewerRequest.currentVersion,
        //     }],
        //   },
        // },
      },
    });
Side question... should remote lambda work for lambda edge functions?
Oh, and here's the current Lambda that I'm trying to attach...
Copy code
export const handler = async event => {
    console.log(JSON.stringify(event, null, 2));
    return event;
};
f
Hey @Tim V,
sst.Function
currently doesn’t support Edge functions.
You’d need to use the
cloudfront.experimental.EdgeFunction
construct to create an Edge function.
^ there’s an open issue for supporting Lambda@Edge native in SST. I’ve bumped up the priority on it https://github.com/serverless-stack/serverless-stack/issues/577
t
Right on. I scoured Slack a while back and found a way to use SST.Functions as Lambda@Edge (that's the viewerRequest.node.host... assignments). I've been using that successfully for Lambda@Edge. I think that my problem today was that I had a bad response from my lambda. I'm getting back to confirming the issue. I should have cross-referenced by custom code before posting in help... but... that's no fun. 🙂