aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

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

Open joel-aws opened 2 years ago

joel-aws commented 2 years ago

What is the problem?

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?

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 .

joel-aws commented 2 years ago

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.

github-actions[bot] commented 1 year ago

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.

joel-aws commented 1 year ago

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

IsmaelMartinez commented 2 months ago

I think this is related to https://github.com/aws/aws-cdk/issues/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.

https://github.com/aws/aws-cdk/blob/8b495f9ec157c0b00674715f62b1bbcabf2096ac/packages/aws-cdk-lib/core/lib/private/synthesis.ts#L249

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