aws/aws-cdk

(Aspects): Aspects not applied to Stage; Aspects applied in Stage does not render Annotations

joel-aws opened this issue · 4 comments

What is the problem?

  • Aspects are not being applied to Stages in an App.
  • Aspects applied within a Stage do not produce Annotation output.

Reproduction Steps

import jsii
from monocdk import (
    App,
    Annotations,
    Aspects,
    Duration,
    Stack,
    Stage,
    Construct,
    IAspect,
    aws_sns as sns,
    aws_sqs as sqs,
    aws_sns_subscriptions as subs,
)

from monocdk_nag import AwsSolutionsChecks

app = App()


class TestStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        queue = sqs.Queue(
            self,
            "DelMeNowQueue",
            visibility_timeout=Duration.seconds(300),
        )

        topic = sns.Topic(self, "DelMeNowTopic")

        topic.add_subscription(subs.SqsSubscription(queue))


@jsii.implements(IAspect)
class NonsenseSQSAspect:
    def visit(self, node):
        if isinstance(node, sqs.Queue):
            print("nonsense")


@jsii.implements(IAspect)
class NonsenseSQSErrorAspect:
    def visit(self, node):
        if isinstance(node, sqs.Queue):
            Annotations.of(node).add_error("foo")


class ApplicationStage(Stage):
    def __init__(
        self,
        scope: Construct,
        id: str,
        **kwargs,
    ):
        super().__init__(scope, id, **kwargs)

        stack = TestStack(self, "test")

        Aspects.of(self).add(AwsSolutionsChecks())  # Test 4 -- Doesn't print output, applies the aspect
        Aspects.of(self).add(NonsenseSQSAspect())  # Test 5 -- Prints output, applies the aspect
        Aspects.of(self).add(NonsenseSQSErrorAspect())  # Test 6 -- Doesn't print output, applies the aspect


stage = ApplicationStage(app, "stage")

Aspects.of(app).add(AwsSolutionsChecks())  # Test 1 -- Doesn't print output or apply the aspect
Aspects.of(app).add(NonsenseSQSAspect())  # Test 2 -- Doesn't print output or apply the aspect
Aspects.of(app).add(NonsenseSQSErrorAspect())  # Test 3 -- Doesn't print output or apply the aspect

app.synth()

What did you expect to happen?

  • Tests 1-3: Aspects to be applied to the Stage in the App and Annotations to produce output.
  • Tests 1 & 4: output from Annotations in Aspects.

What actually happened?

See: What is the problem

CDK CLI Version

1.134.0 (build dd5e12d)

Framework Version

No response

Node.js Version

v16.3.0

OS

macOS 11.5.1

Language

Python

Language Version

No response

Other information

Might be related to #17210 .

Update: with Aspects applied in a Stage (examples 4-6), annotations do appear during a cdk deploy; they do not appear during a cdk synth.

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

Judging by the linked activity, this issue should remain open.

I think this is related to #21341 (or the same underlying issue). My aspects don't work anymore when using Stages that have multiple stacks. I wonder if that is because the for loop that finds the children.

for (const child of construct.node.children) {

As looks like the aspects only iterate throw the Stages, but not beyond that. I will see if I find anything else.