aws-cdk
aws-cdk copied to clipboard
One lambda with two ALBs --> Error: There is already a Construct with name 'InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)' in Function
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)],
});
}
}
Related to #4459
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}