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

Cannot use the same Lambda version for 2 CloudFront distributions #4459

Closed
skinny85 opened this issue Oct 10, 2019 · 5 comments · Fixed by #9966
Closed

Cannot use the same Lambda version for 2 CloudFront distributions #4459

skinny85 opened this issue Oct 10, 2019 · 5 comments · Fixed by #9966
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@skinny85
Copy link
Contributor

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

@skinny85 skinny85 added bug This issue is a bug. @aws-cdk/aws-cloudfront Related to Amazon CloudFront @aws-cdk/aws-lambda Related to AWS Lambda needs-triage This issue or PR still needs to be triaged. labels Oct 10, 2019
@skinny85
Copy link
Contributor Author

cc @skyrpex

@NGL321 NGL321 removed the needs-triage This issue or PR still needs to be triaged. label Oct 10, 2019
@eladb eladb added the p2 label Oct 23, 2019
@eladb eladb assigned iliapolo and unassigned eladb Jan 22, 2020
@ahammond
Copy link
Contributor

ahammond commented Aug 7, 2020

Is there a workaround for this?

@skyrpex
Copy link
Contributor

skyrpex commented Aug 7, 2020

Is there a workaround for this?

You can create as many lambdas as CloudFront distributions you got.

@iliapolo
Copy link
Contributor

iliapolo commented 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:

public get latestVersion(): IVersion {
// Dynamic to avoid infinite recursion when creating the LatestVersion instance...
return new LatestVersion(this);

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

@iliapolo iliapolo assigned njlynch and unassigned iliapolo Aug 19, 2020
@NGL321 NGL321 assigned nija-at and unassigned njlynch Aug 21, 2020
nija-at pushed a commit that referenced this issue Aug 25, 2020
…utions

The error produced is around construct collision, i.e., "There is
already a Construct with name '$LATEST' in Function".

The fix is to cache the latest version on the instance.

fixes #4459
nija-at pushed a commit that referenced this issue Aug 25, 2020
…utions

The error produced is around construct collision, i.e., "There is
already a Construct with name '$LATEST' in Function".

The fix is to cache the latest version on the instance.

fixes #4459
@nija-at nija-at added effort/small Small work item – less than a day of effort p1 and removed p2 @aws-cdk/aws-cloudfront Related to Amazon CloudFront labels Aug 25, 2020
@mergify mergify bot closed this as completed in #9966 Aug 26, 2020
mergify bot pushed a commit that referenced this issue Aug 26, 2020
…utions (#9966)

The error produced is around construct collision, i.e., "There is
already a Construct with name '$LATEST'".

The fix is to cache the latest version on the instance.

fixes #4459


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@talha2021skipq
Copy link

I am getting this error, please help me how to get this error resolved...? I am defining 2 lambda functions in a same stack, is this the reason?
Error: There is already a Construct with name 'AllowInvoke:TalhaProjectStackTalhaSkipQWebHealthTopicA1FC866E' in Function
`
class TalhaProjectStack(cdk.Stack):

def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
    super().__init__(scope, construct_id, **kwargs)
    lambda_role=self.create_lambda_role()
# The code that defines your stack goes here
    HWlambda=self.create_lambda('FirstHWlambda', './resources','webHealth_talha_lambda.lambda_handler' ,lambda_role)
    db_lambda=self.create_lambda('newlambda', './resources','talha_dynamoDb_lambda.lambda_handler' ,lambda_role)

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants