no-construct-stack-suffix β
β
Using recommended in an ESLint configuration enables this rule.
This rule disallows including the strings "Construct" or "Stack" in Construct IDs and Stack IDs.
Including "Construct" in a Construct ID (and similarly for "Stack" in a Stack ID) is discouraged because it can cause issues that should ideally be contained within the CDK environment to leak into the CloudFormation template and the broader AWS environment.
(This rule applies only to classes that extends from Construct or Stack.)
π§ How to use β
ts
// eslint.config.mjs
export default defineConfig([
{
// ... some configs
rules: {
"cdk/no-construct-stack-suffix": "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);
// β
Allowed if the "Construct" and "Stack" suffix are not appended
const bucket = new Bucket(this, "MyBucket");
}
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Stack } from "aws-cdk-lib";
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// β Should not use the "Construct" suffix
const bucket = new Bucket(this, "BucketConstruct");
// β Should not use the suffix "Stack"
new Stack(this, "MyStack");
}
}Options β
ts
type Options = {
disallowedSuffixes: Array<"Construct" | "Stack">;
};
const defaultOptions: Options = {
disallowedSuffixes: ["Construct", "Stack"],
};disallowedSuffixes β
An array of suffixes to disallow. Can include "Construct", "Stack", or both.
With: { disallowedSuffixes: ["Construct"] }
β 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);
// β
Allowed if the "Construct" suffix are not appended
new Stack(this, "MyStack");
}
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Stack } from "aws-cdk-lib";
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// β Should not use the "Construct" suffix
const bucket = new Bucket(this, "BucketConstruct");
}
}