Has anyone used the experimental lambda edge funct...
# help
t
Has anyone used the experimental lambda edge function? Getting some vague immediate errors
f
After the work on NextjsSite, I have some experience w/ it.
t
I was trying the
experimental
version and it's failing with
Error: ENOENT: no such file or directory, copyfile '/home/thdxr/dev/projects/blog/.build/lib/nodejs-entrypoint.js' -> '/home/thdxr/dev/projects/blog/.build/lib/edge-function/__entrypoint__.js'
I'm working around it by using the normal lambda function but then have to setup all the stuff around it manually
like how you have it in the nextjs construct
f
r u deploying to us-east-1?
t
I just switched it to that
f
ah i see.. yeah if u r deploying to us-east-1, it’s better to use the Lambda function directly
t
We have this code in our docs
Copy code
import { LambdaEdgeEventType, experimental } from "@aws-cdk/aws-cloudfront";

const edgeFunc = new experimental.EdgeFunction(this, "MyFunction", {
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: "lambda.handler",
  code: lambda.Code.fromAsset("path/to/dir"),
  stackId: `${scope.logicalPrefixedName("edge-lambda")}`,
});

new StaticSite(this, "Site", {
  path: "path/to/src",
  cfDistribution: {
    defaultBehavior: {
      edgeLambdas: [
        {
          functionVersion: edgeFunc.currentVersion,
          eventType: LambdaEdgeEventType.VIEWER_RESPONSE,
        },
      ],
    },
  },
});
I don't think it works for me
I tried it in us-east-2
f
hmm where do u think
lib/nodejs-entrypoint.js'
comes from?
t
I think maybe they spawn a custom resource to create the function cross region:
Copy code
Error: ENOENT: no such file or directory, copyfile '/home/thdxr/dev/projects/blog/.build/lib/nodejs-entrypoint.js' -> '/home/thdxr/dev/projects/blog/.build/lib/edge-function/__entrypoint__.js'
    at Object.copyFileSync (node:fs:2792:3)
    at new CustomResourceProvider (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts:86:8)
    at Function.getOrCreateProvider (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts:69:10)
    at Function.getOrCreate (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts:61:17)
    at EdgeFunction.createCrossRegionArnReader (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/aws-cloudfront/lib/experimental/edge-function.ts:162:49)
    at EdgeFunction.createCrossRegionFunction (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/aws-cloudfront/lib/experimental/edge-function.ts:144:26)
    at new EdgeFunction (/home/thdxr/dev/projects/blog/node_modules/@aws-cdk/aws-cloudfront/lib/experimental/edge-function.ts:43:14)
I need to try it again using us-east-1
t
yeah it works from us-east-1
since they can skip that
Copy code
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(props.codeDirectory, `${ENTRYPOINT_FILENAME}.js`));
this is giving the error
props.codeDirectory
comes from experimental function code
Copy code
const serviceToken = CustomResourceProvider.getOrCreate(this, resourceType, {
      codeDirectory: path.join(__dirname, 'edge-function'),
      runtime: CustomResourceProviderRuntime.NODEJS_12_X,
      policyStatements: [{
        Effect: 'Allow',
        Resource: parameterArnPrefix,
        Action: ['ssm:GetParameter'],
      }],
    });
__dirname
is resolved to the
.build
directory
t
Ah
f
I forgot if we had a solution for this… I remember this has been brought up b4
t
Well turns out for what I wanted to do cloudfront functions was a better fit
and that worked easily out of the box
a
@thdxr I’ve tried to use but got errors.
Did you guys figured out?
I need to do something too.
t
Ended up using CLoudfront functions because they fit my needs
a
😧
t
I got around the errors by deploying straight into us-east-1
a
Yeah I wanted to try both, I want to read “user country” header.
t
depending on what you need to do Cloudfront Functions are much simpler
a
I want this header from CFront:
CloudFront-Viewer-Country
.
Yeah.