aws-cdk

CDK only deploy first stack - getting "No stacks match the name(s)" for others


I'm running into a weird issue with CDK. I have three envs for a services, defined as stacks. I have previously deployed all three of them (each in an individual account). Each stack shows up in CloudFormation.

I can deploy the first stack fine (cdk deploy myapp-service-development). However when I run cdk deploy myapp-service-test I'm getting the error No stacks match the name(s) myapp-service-test. When I comment the first stack (developemt) out, I can deploy the next stack (test) and so on.

The command cdk ls only ever gives me the first stack defined in the file below:

#!/usr/bin/env node
import { devEnv, testEnv, prodEnv } from "@my-app/configuration";
import { MyStack } from "../src/infrastructure/index.js";
import * as cdk from "aws-cdk-lib";

const app = new cdk.App();

new MyStack(app, `myapp-service-development`, {
  stackName: "service",
  env: devEnv, // this is { account: "someAccountId", region: "eu-central-1"}
});

new MyStack(app, "myapp-service-test", {
  stackName: "service",
  env: testEnv,
});

new MyStack(app, `myapp-service-production`, {
  stackName: "service",
  env: prodEnv,
});



Solution

  • This is my current workaround which let me deploys the individual stacks.

    #!/usr/bin/env node
    import { devEnv, testEnv, prodEnv } from "@my-app/configuration";
    import { MyStack } from "../src/infrastructure/index.js";
    import * as cdk from "aws-cdk-lib";
    
    const app = new cdk.App();
    
    const bundlingStacks = app.node.tryGetContext("aws:cdk:bundling-stacks") as Array<string>;
    const buildAllStacks = bundlingStacks.includes("**");
    
    if (buildAllStacks || bundlingStacks.includes(`easypliant-${serviceName}-development`)) {
      new MyStack(app, `myapp-service-development`, {
        stackName: "service",
        env: devEnv, // this is { account: "someAccountId", region: "eu-central-1"}
      });
    }
    
    if (buildAllStacks || bundlingStacks.includes(`easypliant-${serviceName}-test`)) {
      new MyStack(app, "myapp-service-test", {
        stackName: "service",
        env: testEnv,
      });
    }
    
    if (buildAllStacks || bundlingStacks.includes(`easypliant-${serviceName}-production`)) {
      new MyStack(app, `myapp-service-production`, {
        stackName: "service",
        env: prodEnv,
      });
    }