question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws-cloudfront): few questions regarding Lambda@Edge function/stack

See original GitHub issue

❓ General Issue

The Question

Hello, @njlynch.

I have a few questions regarding the new Lambda@Edge functionality.

  1. When I try to create a L@E function, my stack is named *-us-west-2. Is this expected behavior? My understanding is that L@E stack will be deployed to us-east-1. My default region is us-west-2 when I run cdk doctor.
Successfully synthesized to /Users/robertd/code/cf/static-cloudfront-distributions/cdk.out
Supply a stack id (edge-lambda-stack-us-west-2, demo-appsStaticCFStack) to display its template.
  1. If I create multiple L@E functions for the particular CF distro should I expect to see multiple CFN stacks for L@E functions? I see that everything is lumped under one stack right now.

  2. I’m building a factory-style CDK application that will create multiple CloudFront distros with accompanying L@E functions (per distro) based on the configuration file. If these edge functions are all being lumped under the same stack, how do I keep them siloed (per CF distro) if their stacks are all named the same (i.e.: edge-lambda-stack-us-west-2).

Example (it doesn’t paint the whole picture but this is what I’m trying to do):

    const bucket = props.distroConfig.existingBucket ? s3.Bucket.fromBucketName(this, "existing-bucket", props.distroConfig.existingBucket) 
      : createBucket(this, props.distroConfig.stackName, this.account, logBucket);

    const edgeLambdas = props.distroConfig.lambdaEdgeConfig ? props.distroConfig.lambdaEdgeConfig.map((config, index) => {
      const edgeLambda = new cloudfront.experimental.EdgeFunction(this, `${props.distroConfig.stackName}-lambda-edge-${index}`, {
        runtime: config.runtime,
        handler: 'index.handler',
        code: lambda.Code.fromAsset(path.join(__dirname, 'lambda')),
        description: `Lambda Edge function for ${props.distroConfig.stackName}`,
      });
      
      return {
        functionVersion: edgeLambda.currentVersion,
        eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
      }
    }) : undefined;

    const distro = new cloudfront.Distribution(this, "distro", {
      defaultBehavior: {
        origin: new origins.S3Origin(bucket),
        allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
        // cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED, // default: CACHING_OPTIMIZED 24hr
        cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
        viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
        edgeLambdas,
      },
      certificate,
      comment: `${props.distroConfig.domainNames[0]} cloudfront distribution.`,
      logBucket: logBucket,
      logFilePrefix: `AWSLogs/${this.account}/cloudfront/${props.distroConfig.team}/static/${props.distroConfig.stackName}`,
      logIncludesCookies: true,
      defaultRootObject: "index.html",
      priceClass: cloudfront.PriceClass.PRICE_CLASS_100, // USA & EU
      domainNames: props.distroConfig.domainNames,
    });

Thanks! ✋

Environment

  • CDK CLI Version: 1.82.0
  • Node.js Version: 15.5.0
  • OS: macOS Mojave
  • Language (Version): TypeScript (4.1.3)

Other information

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
njlynchcommented, Jan 4, 2021

Thanks for the feedback and questions! Questions 4 and 5 require a slightly deeper look; I expect we’ll end up with separate bug/feature requests for each, but let me do a quick probe into them first.

On the subjects of the L@E stack name:

  1. When I try to create a L@E function, my stack is named *-us-west-2. Is this expected behavior? My understanding is that L@E stack will be deployed to us-east-1.
  2. If I create multiple L@E functions for the particular CF distro should I expect to see multiple CFN stacks for L@E functions? I see that everything is lumped under one stack right now.
  3. […] If these edge functions are all being lumped under the same stack, how do I keep them siloed (per CF distro) if their stacks are all named the same (i.e.: edge-lambda-stack-us-west-2).

Yes, the L@E stack will be deployed to us-east-1. The stack is named *-us-west-2, to indicate its origin. Your “primary”/CloudFront stack is in us-west-2, so the us-east-1 stack gets that name to tell you what region spawned the stack. This maybe wasn’t the clearest naming on my part. 😕

The current implementation was targeted at “simple” cases with a single CloudFront distro (per region), which is the most common app type I’ve seen; as such, I picked a naming convention I thought was simplest for the “base case”. However, in your case, this clearly won’t work terribly well. One solution might be a bug fix to have the stack named better (perhaps using the “parent” stack name as a seed); I’m open to suggestions here on what naming might make sense. The other approach for this case is #12136, which will allow you to explicitly specify the stack name for each L@E stack (this will go out with the next release).

0reactions
github-actions[bot]commented, Jan 27, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda@Edge example functions - Amazon CloudFront
The following example shows how to use a Lambda function to serve static website content, which reduces the load on the origin server...
Read more >
Managing Lambda@Edge and CloudFront deployments by ...
Step 1: Use AWS CloudFormation to deploy and modify a Lambda@Edge function associated with a CloudFront Distribution; Step 2: Create a CI/CD ...
Read more >
Lambda@Edge event structure - Amazon CloudFront
The following topics show the structure of the object that CloudFront passes to a Lambda function for viewer and origin request events.
Read more >
Customizing at the edge with Lambda@Edge
Lambda @Edge is an extension of AWS Lambda, a compute service that lets you execute functions that customize the content that CloudFront delivers....
Read more >
Setting IAM permissions and roles for Lambda@Edge
By default, whenever a CloudFront event triggers a Lambda function, data is written to CloudWatch Logs. If you want to use these logs,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found