Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDK: switch to CloudFront EdgeFunction #951

Open
arjunnaha opened this issue Mar 16, 2021 · 5 comments
Open

CDK: switch to CloudFront EdgeFunction #951

arjunnaha opened this issue Mar 16, 2021 · 5 comments

Comments

@arjunnaha
Copy link

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:

  • version aliases clash (issue) fixed
  • role circular dependency (issue)

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.

@arjunnaha arjunnaha changed the title CDK: switch to CloudFront EdgeFucntion CDK: switch to CloudFront EdgeFunction Mar 16, 2021
@kirkness
Copy link
Collaborator

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.

@bobrossthepainter
Copy link

bobrossthepainter commented Sep 2, 2021

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

@w0otness
Copy link

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,
    

@ChristopheBougere
Copy link

ChristopheBougere commented Apr 28, 2022

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);
  });

@devlsh
Copy link

devlsh commented Jun 2, 2022

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants