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.

Cannot use the same Lambda version for 2 CloudFront distributions

See original GitHub issue

From #4237:

Reproduction Steps

import * as cdk from "@aws-cdk/core";
import * as s3 from "@aws-cdk/aws-s3";
import * as lambda from "@aws-cdk/aws-lambda";
import * as cloudfront from "@aws-cdk/aws-cloudfront";

class SharedPwaLambdasStack extends cdk.Stack {
  addCspHeadersFunction: lambda.Function;

  constructor(scope: cdk.Construct, name: string, props?: cdk.StackProps) {
    super(scope, name, props);

    this.addCspHeadersFunction = new lambda.Function(this, "AddCspHeaders", {
      runtime: lambda.Runtime.NODEJS_8_10,
      handler: "index.handler",
      code: lambda.Code.fromInline("irrelevant"),
    });
  }
}

interface PwaStackProps extends cdk.StackProps {
  addCspHeadersFunction: lambda.Function;
}

class PwaStack extends cdk.Stack {
  constructor(scope: cdk.Construct, name: string, props: PwaStackProps) {
    super(scope, name, props);

    const bucket = new s3.Bucket(this, `${name}Bucket`);

    const distribution = new cloudfront.CloudFrontWebDistribution(this, `${name}Distribution`, {
      originConfigs: [
        {
          s3OriginSource: {
            s3BucketSource: bucket,
          },
          behaviors: [
            {
              isDefaultBehavior: true,
              lambdaFunctionAssociations: [
                {
                  eventType: cloudfront.LambdaEdgeEventType.VIEWER_RESPONSE,
                  lambdaFunction: props.addCspHeadersFunction.latestVersion,
                },
              ],
            },
          ],
        },
      ],
    });
  }
}

const app = new cdk.App();

const sharedPwaLambdasStack = new SharedPwaLambdasStack(app, "SharedPwaLambdas");

const websitePwa = new PwaStack(app, "WebsitePwa", {
  addCspHeadersFunction: sharedPwaLambdasStack.addCspHeadersFunction,
});

// If you comment the following stack, it will work.
const adminPwa = new PwaStack(app, "AdminPwa", {
  addCspHeadersFunction: sharedPwaLambdasStack.addCspHeadersFunction,
});

Error Log

cdk synth
yarn run v1.19.0
$ /code/node_modules/.bin/ts-node index.ts

/code/node_modules/@aws-cdk/core/lib/construct.ts:512
      throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^
Error: There is already a Construct with name '$LATEST' in Function [AddCspHeaders]
    at ConstructNode.addChild (/code/node_modules/@aws-cdk/core/lib/construct.ts:512:13)
    at new ConstructNode (/code/node_modules/@aws-cdk/core/lib/construct.ts:144:18)
    at new Construct (/code/node_modules/@aws-cdk/core/lib/construct.ts:563:17)
    at new Resource (/code/node_modules/@aws-cdk/core/lib/resource.ts:61:5)
    at new FunctionBase (/code/node_modules/@aws-cdk/aws-lambda/lib/function-base.ts:139:1)
    at new LatestVersion (/code/node_modules/@aws-cdk/aws-lambda/lib/function-base.ts:336:5)
    at Function.get latestVersion [as latestVersion] (/code/node_modules/@aws-cdk/aws-lambda/lib/function-base.ts:233:12)
    at new PwaStack (/code/cloud/index.ts:42:63)
    at Object.<anonymous> (/code/cloud/index.ts:62:18)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Environment

  • CLI Version :
  • Framework Version:
  • OS :
  • Language :

Other


This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
iliapolocommented, Aug 9, 2020

I believe the problem is with the dual reference to props.addCspHeadersFunction.latestVersion, which creates multiple instances, with the same scope and id:

https://github.com/aws/aws-cdk/blob/9ff55aeeed49d89bf13b2baf9025a1f4e038aa43/packages/%40aws-cdk/aws-lambda/lib/function-base.ts#L246-L248

A workaround could be storing the latest version in a const, and passing an IVersion, instead of a Function:

const sharedPwaLambdasStack = new SharedPwaLambdasStack(app, "SharedPwaLambdas");

const latestVersion = sharedPwaLambdasStack.addCspHeadersFunction.latestVersion;

const websitePwa = new PwaStack(app, "WebsitePwa", {
  addCspHeadersFunctionVersion: latestVersion,
});

// If you comment the following stack, it will work.
const adminPwa = new PwaStack(app, "AdminPwa", {
  addCspHeadersFunctionVersion: latestVersion,
});
1reaction
skinny85commented, Oct 10, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Associating functions with distributions - Amazon CloudFront
To use a function in CloudFront Functions with a CloudFront distribution, you associate the function with one or more cache behaviors in the...
Read more >
how do i delete a lambda@edge that isn't attached to a ...
I only have two cloudfront distributions in my account, neither of them reference my edge lambda, and yet when I try to delete...
Read more >
The Lambda function associated with the CloudFront ...
For me, using the Alpha version of Serverless, solved this error message 'The Lambda function associated with the CloudFront distribution is ...
Read more >
Lambda@Edge example functions - Amazon CloudFront
The examples in this section illustrate some common ways to use Lambda@Edge in CloudFront. Topics. Example: A/B testing; Example: Overriding a response header ......
Read more >
How to Set up a Lambda@Edge Function - YouTube
Learn more about Lambda @Edge and the most common use cases here: https://amzn.to/2RSHVVcLearn how to set up an AWS Lambda @Edge function.
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