prevent-construct-id-collision β
βΉοΈ This rule will be included in recommended rules in v5.0.0.
This rule prohibits the use of static strings (string literals) for the IDs of Constructs instantiated within loop processing. This prevents Construct ID collisions.
If you specify a literal string for the IDs of a Construct instantiated within a loop (e.g. for, forEach, map), an attempt will be made to create Constructs with the same ID in every iteration. Since Construct IDs must be unique within a specific scope, this causes deployment errors.
π§ How to use β
js
// eslint.config.mjs
export default defineConfig([
{
// ... some configs
rules: {
"awscdk/prevent-construct-id-collision": "error",
},
},
]);β Correct Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// β
Literal ID outside of a loop
new Bucket(this, "Bucket");
// β
Variable ID inside forEach
["Id1", "Id2", "Id3"].forEach((item) => new Bucket(this, item));
// β
Variable ID with template literal inside a loop
const items = ["a", "b", "c"];
for (const item of items) {
new Bucket(this, `${item}Bucket`);
}
}
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// β Literal ID inside forEach β all iterations create "Bucket", causing collision
[1, 2, 3].forEach(() => new Bucket(this, "Bucket"));
// β Literal ID inside for loop β same collision issue
for (let i = 0; i < 3; i++) {
new Bucket(this, "Bucket");
}
// β Using a template literal without expressions inside a loop - same collision issue
["a", "b", "c"].forEach(() => new Bucket(this, `Bucket`));
}
}