AWS CDK ERROR: There is already a Construct with name ‘XYZ’
- By : Mydatahack
- Category : AWS, Infrastructure
- Tags: AWS, CDK, Troubleshoot
When I was running AWS CDK, I got this error and couldn’t really find the answer when I googled it. This error might happen for different reasons, but I worked out what was wrong with my CDK code after a few hours of googling and trying out different things. I’m hoping this post will save you hours going crazy on this mystery error message.
Error
Error is thrown in construct.ts as below.
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 'XYZ' in XYZStack
Solution
In stack, each construct requires to have a unique id. In my original code, I was just passing the id two both construct, which was causing this error.
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // The code that defines your stack goes here new S3Bucket(this, id); new ConnectDataKinesisStream(this, id); }
The solution to make this id unique. I added the resource suffix. But, this can be whatever as long as each construct has different id.
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // The code that defines your stack goes here new S3Bucket(this, id + '-s3'); new ConnectDataKinesisStream(this, id + '-kinesis'); }
This is it. Error’s gone. Happy building!