Skip to content

no-parent-name-construct-id-match

✅ Using recommended in an ESLint configuration enables this rule.

This rule disallows using the parent class name as the Construct ID.

It is not recommended to specify a string that matches (or includes) the parent class name for the Construct ID, as it can make the CloudFormation resource hierarchy unclear.


🔧 How to use

js
// eslint.config.mjs
export default defineConfig([
  {
    // ... some configs
    rules: {
      "awscdk/no-parent-name-construct-id-match": "error",
    },
  },
]);

✅ Correct Example

ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // ✅ Can use a different name from the parent construct
    const bucket = new Bucket(this, "MyBucket");

    // ✅ Construct ID containing the parent class name can be used
    const bucket = new Bucket(this, "MyConstructBucket");
  }
}

❌ Incorrect Example

ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // ❌ Construct ID should not exactly match the parent class name
    const bucket = new Bucket(this, "MyConstruct");
  }
}

Options

ts
type Options = {
  disallowContainingParentName: boolean;
};

const defaultOptions: Options = {
  disallowContainingParentName: false,
};

disallowContainingParentName

When true, disallows using construct IDs that contain the parent class name. When false, using construct IDs that contain the parent class name is allowed, but using construct IDs that exactly match the parent class name is disallowed.

With: { disallowContainingParentName: true }

✅ Correct Example

ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // ✅ Can use a different name from the parent construct
    const bucket = new Bucket(this, "MyBucket");
  }
}

❌ Incorrect Example

ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // ❌ Construct ID should not exactly match the parent class name
    const bucket = new Bucket(this, "MyConstruct");

    // ❌ Construct ID should not include the parent class name
    const bucket = new Bucket(this, "MyConstructBucket");
  }
}