serverless-next.js
serverless-next.js copied to clipboard
CDK: switch to CloudFront EdgeFunction
Is your feature request related to a problem? Please describe. At the moment, CDK stacks have to be deployed to us-east-1 because of the Lambda@Edge function.
Describe the solution you'd like
The CloudFront module provides a (experimental) EdgeFunction module that behaves exactly like a normal Function, however if the stack is deployed to a region that isn't us-east-1, it will isolate the function into a new stack and create the function in the correct region.
Current blockers
I've tried to switch to EdgeFunction locally, and there are a few issues:
Re the circular dependency, the only solution seems to be using another stack and importing the role in. We could have 'support' constructs that go into a separate shared stack, which would also solve #947.
Hey Arjun, this sounds great. I'm hesitant to jump in a use an experimental module, although either way, I'll have a play with this myself.
Hi, first of all thanks for this super nice sls-component and the cdk adapter! Especially with being able to configure regions for S3 and SQS, it boosts up the performance of our webapp immensely. Back to the topic:
I just tried the switch to EdgeFunction by myself and it works in my scenario (just using the regeneration and default lambda). I'm deploying the Stack in eu-central-1 and with switching all lambdas to EdgeFunctions (except the regeneration lambda), a us-1-region parent stack is automatically generated, containing the edge lambdas. Furthermore, to overcome the circular role issue, I simply removed the custom role assigned to the edge lambdas, because the addition of the "edgelambda.amazonaws.com" Principal seems to be implicit for EdgeFunctions.
Cheers Rob
The role it assigns is the default one given... I just commented out the manual role assignment and it works.
diff --git a/node_modules/@sls-next/cdk-construct/dist/index.js b/node_modules/@sls-next/cdk-construct/dist/index.js
-
this.defaultNextLambda = new lambda.Function(this, "NextLambda", {
-
this.defaultNextLambda = new aws_cloudfront_1.experimental.EdgeFunction(this, "NextLambda", {
-
role: this.edgeLambdaRole,
-
// role: this.edgeLambdaRole,
Thanks for the workaround @w0otness 👌
For the maintainers, would you accept a PR that would propose this change as an option in the construct props?
Meanwhile, here is a snippet of the patch if anyone is interested:
import * as fs from 'fs';
import * as path from 'path';
import * as cdk from 'aws-cdk-lib';
async function patchConstructCode() {
const filePath = path.join(__dirname, 'node_modules/@sls-next/cdk-construct/dist/index.js');
const fileContent = await (await fs.promises.readFile(filePath)).toString();
await fs.promises.writeFile(
filePath,
fileContent.replace('lambda.Function(this, "NextLambda"', 'aws_cloudfront_1.experimental.EdgeFunction(this, "NextLambda"')
.replace('role: this.edgeLambdaRole', '// role: this.edgeLambdaRole'),
);
}
patchConstructCode()
.then(() => import('@sls-next/lambda-at-edge'))
.then(({ Builder }) => {
const builder = new Builder('.', './build', { args: ['build'] });
builder.build();
})
.then(() => {
const app = new cdk.App();
new MyStack(app, 'MyStack');
})
.catch((e) => {
console.log(e);
process.exit(1);
});
Any updates on this?