Skip to content

pipelines: Runtime error when a Stage has multiple Stacks with the same stack name #30449

@kmonihen

Description

@kmonihen

Describe the bug

When a stage contains multiple stacks with different IDs and different environments but the same name, cdk will throw a runtime error RuntimeError: Error: Node with duplicate id: <stack_name>. This error occurs during synth when the stage is added to a cdk pipeline.

Expected Behavior

The cdk pipeline should be able to handle a stage with multiple stacks of the same name when the IDs and environments are different.

Current Behavior

Stacks with the same name but different IDs in the same stage cause a runtime error:

jsii.errors.JavaScriptError:
  @jsii/kernel.RuntimeError: Error: Node with duplicate id: my-test-stack
      at Kernel._Kernel_ensureSync (/tmp/tmpf9k6132x/lib/program.js:10502:23)
      at Kernel.invoke (/tmp/tmpf9k6132x/lib/program.js:9866:102)
      at KernelHost.processRequest (/tmp/tmpf9k6132x/lib/program.js:11707:36)
      at KernelHost.run (/tmp/tmpf9k6132x/lib/program.js:11667:22)
      at Immediate._onImmediate (/tmp/tmpf9k6132x/lib/program.js:11668:46)
      at processImmediate (node:internal/timers:466:21)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/workspaces/cdk_bug/bug.py", line 54, in <module>
    app.synth()
  File "/home/vscode/.local/lib/python3.9/site-packages/aws_cdk/__init__.py", line 21373, in synth
    return typing.cast(_CloudAssembly_c693643e, jsii.invoke(self, "synth", [options]))
  File "/home/vscode/.local/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 149, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/home/vscode/.local/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 399, in invoke
    response = self.provider.invoke(
  File "/home/vscode/.local/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 380, in invoke
    return self._process.send(request, InvokeResponse)
  File "/home/vscode/.local/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 342, in send
    raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: Error: Node with duplicate id: my-test-stack

Reproduction Steps

This app has a cdk pipeline and a single stage with 2 stacks. The stack IDs and environments differ but the stack names are the same. Synth this app as-is and it will generate an error: RuntimeError: Error: Node with duplicate id: my-test-stack. Modify one of the stack names (like "my-test-stack-2") and it will synth without error.

from constructs import Construct
from aws_cdk import App, Environment, Stack, Stage
from aws_cdk import pipelines


class DeploymentStage(Stage):

    def __init__(self, scope: Construct, stage_id: str):
        super().__init__(scope, stage_id)
        Stack(
            self,
            "StackEnv1",
            stack_name="my-test-stack",
            env=Environment(
                account="123456789012",
                region="us-east-1",
            ),
        )
        Stack(
            self,
            "StackEnv2",
            stack_name="my-test-stack",
            env=Environment(
                account="210987654321",
                region="us-east-1",
            ),
        )


app = App()

pipeline_stack = Stack(
    app,
    "my-test-pipeline",
    env=Environment(account="657849302012", region="us-east-1"),
)

pipeline = pipelines.CodePipeline(
    pipeline_stack,
    "Pipeline",
    cross_account_keys=True,
    synth=pipelines.ShellStep("Synth",
        input=pipelines.CodePipelineSource.connection(
            "my-org/my-app",
            "main",
            connection_arn="arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41",
        ),
        commands=["npm ci", "npm run build", "npx cdk synth"],
    ),
)

pipeline.add_stage(DeploymentStage(pipeline_stack, "Production"))

app.synth()

Possible Solution

No response

Additional Information/Context

The stack templates and manifest files are generated properly in the cdk.out folder under the pipeline assembly. Each stack has it's own asset and template files differentiated by stack ID and hash.

CDK CLI Version

2.144.0 (build 5fb15bc)

Framework Version

2.144.0

Node.js Version

18.17.0

OS

rocky linux 9

Language

Python

Language Version

3.9.18

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/pipelinesCDK Pipelines librarybugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions