aws/aws-cdk

aws_cdk.Annotations: annotations not applied if using stages

Closed this issue · 4 comments

Describe the bug

Annotations are not applied when a Stack is deployed as part of a Stage. This seems to be related to #9799.

This in turn was a duplicate of #8676 which was closed as fixed, but the annotation processing is not applied as demonstrable by the code snippet below.

This is on CDK 2.46.0 (build 5a0595e).

Expected Behavior

I expected to see the annotations in the CDK Synth output

cdk synth 
[Error at /Stage-WorkingStack] this will not appear

Found errors

Current Behavior

I observe no annotations (warnings, info, error) when using stacks deployed as part of a pipeline

cdk synth 
Successfully synthesized to /Users/paulmiller/Design/wfo-codedeploy/cdk.out
Supply a stack id () to display its template.

Reproduction Steps

app.py

import aws_cdk as cdk
from constructs import Construct


class Stack(cdk.Stack):
    def __init__(self, scope: Construct, construct_id: str):
        super().__init__(scope, construct_id)

        cdk.Annotations.of(self).add_error("this will not appear when called from a stage")


def ok():
    app = cdk.App()
    stack = Stack(app, "WorkingStack")
    return app.synth()


def fails():
    app = cdk.App()
    stage = cdk.Stage(app, "Stage")
    stack = Stack(stage, "FailingStack")
    return app.synth()


if __name__ == "__main__":
    fails()

cdk.json

{
  "app": "python3 app.py"
}

Running this with cdk synth will show no annotation when running the fails() app. Running the ok() app works.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.46.0 (build 5a0595e)

Framework Version

No response

Node.js Version

v18.10.0

OS

MacOS Monterey

Language

Python

Language Version

Python (3.10.6)

Other information

No response

Thanks for opening this issue @pwrmiller,

This is happening because you aren't actually synthing the stack inside the stage with cdk synth. Since the stack is inside a stage, it isn't a top level stack which means it's not going to synthesize unless specified. To truly specify all stacks, you can run cdk synth '**', or you can specify the stack in your stage like cdk synth Stage/*. Both of these will produce the error when ran 🙂

To see more information on specifying stacks, see the documentation here

⚠️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.

@peterwoodworth Is the prepare phase for a stack within a Stage run with a bare cdk synth?

Context:
Trying to understand the behavior in this cdk-nag issue cdklabs/cdk-nag#637

If the stack isn't specified it's going to be completely ignored @dontirun. You need to specify the stack as I've indicated above - top level stages are not synthesized by default. That issue you've linked just looks to me like they need to actually specify the stack (like in my above comment)