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

One lambda with two ALBs --> Error: There is already a Construct with name 'InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)' in Function #8553

Closed
AnatolyUA opened this issue Jun 15, 2020 · 2 comments · Fixed by #9960
Assignees
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing @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 in-progress This issue is being actively worked on. p1

Comments

@AnatolyUA
Copy link

The Question

When I'm trying to add one lambda function to two Application Load Balancers I'm getting this error:

\node_modules\constructs\lib\construct.ts:487
      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 'InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)' in Function [SharedLambda]
    at Node.addChild (shared-lambda\node_modules\constructs\lib\construct.ts:487:13)
    at new Node (shared-lambda\node_modules\constructs\lib\construct.ts:73:22)
    at new ConstructNode (shared-lambda\node_modules\@aws-cdk\core\lib\construct-compat.ts:265:24)
    at Object.createNode (shared-lambda\node_modules\@aws-cdk\core\lib\construct-compat.ts:71:11)
    at new Construct (shared-lambda\node_modules\constructs\lib\construct.ts:528:26)
    at new Construct (shared-lambda\node_modules\@aws-cdk\core\lib\construct-compat.ts:68:5)
    at new CfnElement (shared-lambda\node_modules\@aws-cdk\core\lib\cfn-element.ts:54:5)
    at new CfnRefElement (shared-lambda\node_modules\@aws-cdk\core\lib\cfn-element.ts:145:1)
    at new CfnResource (shared-lambda\node_modules\@aws-cdk\core\lib\cfn-resource.ts:83:5)
    at new CfnPermission (shared-lambda\node_modules\@aws-cdk\aws-lambda\lib\lambda.generated.ts:2649:9)
Subprocess exited with error 1

This looks like a bug because there are no problems when I'm doing this in Console.
Or maybe I'm doing something wrong?

Environment

  • CDK CLI Version: 1.45.0 (build 0cfab15)
  • Module Version: 1.45.0
  • Node.js Version: v12.16.1
  • OS: all
  • Language (Version): all
export class SharedLambdaStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'VpcForAlbs', {
      cidr: '10.0.0.0/16',
      subnetConfiguration: [{
        cidrMask: 28,
        name: 'Public',
        subnetType: ec2.SubnetType.PUBLIC,
      }], 
      maxAzs: 2, 
    })

    const sharedLambda = new lambda.Function(this, 'SharedLambda', {
      runtime: lambda.Runtime.NODEJS_12_X,
      code: lambda.Code.fromAsset('lambda'),
      handler: 'hello.handler'
    })

    this.addLb('FirstAlb', vpc, sharedLambda);
    this.addLb('SecondAlb', vpc, sharedLambda);
  }

  private addLb(id: string, vpc: IVpc, func: lambda.Function) {
    const lb = new elbv2.ApplicationLoadBalancer(this, id, {
      vpc,
      internetFacing: true
    });

    const listener = lb.addListener(`${id}-Listener`, { port: 80, open: true });
    listener.addTargets(`${id}-Targets`, {
      targets: [new targets.LambdaTarget(func)],
    });
  }
}
@AnatolyUA AnatolyUA added the needs-triage This issue or PR still needs to be triaged. label Jun 15, 2020
@SomayaB SomayaB added the @aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing label Jun 17, 2020
@SomayaB SomayaB added bug This issue is a bug. guidance Question that needs advice or information. and removed guidance Question that needs advice or information. labels Jun 17, 2020
@rix0rrr rix0rrr added the p1 label Jun 18, 2020
@NetaNir NetaNir added the effort/small Small work item – less than a day of effort label Aug 10, 2020
@SomayaB SomayaB assigned njlynch and unassigned rix0rrr Aug 20, 2020
@SomayaB SomayaB added the @aws-cdk/aws-lambda Related to AWS Lambda label Aug 21, 2020
@SomayaB
Copy link
Contributor

SomayaB commented Aug 21, 2020

Related to #4459

@njlynch njlynch removed the needs-triage This issue or PR still needs to be triaged. label Aug 24, 2020
njlynch added a commit that referenced this issue Aug 25, 2020
If `grantInvoke()` is called twice for the same principal, the second call fails
due to attempting to create two `CfnPermission` nodes with the same id. This
(simple) fix skips the second creation if the node already exists.

A more robust check would be to check the existing `CfnPermission`, and compare
every field, skipping creation if the two are identical and throwing an error
otherwise, as well as handling that in the upstream `grantInvoke` call. I opted
for the simpler solution for now, but willing to take arguments for something
more complex.

fixes #8553
njlynch added a commit that referenced this issue Aug 25, 2020
If `grantInvoke()` is called twice for the same principal, the second call fails
due to attempting to create two `CfnPermission` nodes with the same id. This
(simple) fix skips the second creation if the node already exists.

A more robust check would be to check the existing `CfnPermission`, and compare
every field, skipping creation if the two are identical and throwing an error
otherwise, as well as handling that in the upstream `grantInvoke` call. I opted
for the simpler solution for now, but willing to take arguments for something
more complex.

I also nested the existing grantInvoke tests for future readability.

fixes #8553
@njlynch njlynch added the in-progress This issue is being actively worked on. label Aug 25, 2020
@mergify mergify bot closed this as completed in #9960 Aug 27, 2020
mergify bot pushed a commit that referenced this issue Aug 27, 2020
If `grantInvoke()` is called twice for the same principal, the second call fails
due to attempting to create two `CfnPermission` nodes with the same id. This
(simple) fix skips the second creation if the node already exists.

A more robust check would be to check the existing `CfnPermission`, comparing
every field, skipping creation if the two are identical and throwing an error
otherwise, as well as handling that in the upstream `grantInvoke` call. I opted
for the simpler solution for now, but willing to take arguments for something
more complex.

I also nested the existing grantInvoke tests for future readability. The tests weren't changed,
just the last one added.

fixes #8553


----

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

I am getting same issue for apigateway , My requirement is to create two api gateway from same construct
There is already a Construct with name '${childName}' in ${typeName}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing @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 in-progress This issue is being actively worked on. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants