Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
kmonihen opened this issue Jun 4, 2024 · 4 comments
Labels
@aws-cdk/pipelines CDK Pipelines library bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@kmonihen
Copy link

kmonihen commented Jun 4, 2024

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

@kmonihen kmonihen added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 4, 2024
@github-actions github-actions bot added the @aws-cdk/pipelines CDK Pipelines library label Jun 4, 2024
@kmonihen
Copy link
Author

kmonihen commented Jun 6, 2024

It looks like waves are the solution to this.

@kmonihen kmonihen closed this as completed Jun 6, 2024
Copy link

github-actions bot commented Jun 6, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@UnbiasedGoat
Copy link

UnbiasedGoat commented Jun 18, 2024

It looks like waves are the solution to this.

@kmonihen

I have the same issue - I'm not sure I agree that waves are the solution here - isn't that just for parallel stages? So you'd have to split your stacks into multiple stages still? You can maintain the parallel deploys but you still have the same problem if you don't split it up - could we maybe re-open?

@aws-cdk-automation
Copy link
Collaborator

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
@aws-cdk/pipelines CDK Pipelines library bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants