CfnTopic Python AWS CDK 访问主题 ARN?

CfnTopic Python AWS CDK access Topic ARN?

给定一个使用 AWS CDK 的 CfnTopic Python,如何访问主题 ARN 属性?我可以使用更高级别的 Topic 构造访问此 属性,但不能使用较低级别的 CfnTopic 构造。

from aws_cdk.aws_sns import CfnTopic, Topic
from constructs import Construct

class TopicConstruct(Construct):
    def __init__(self, scope: Construct, id: str):
        topic = CfnTopic(self, id="Topic")  # <- Can't access topic.topic_arn
        topic_construct = Topic(self, id="OtherTopic")
        topic_construct_arn = topic_construct.topic_arn  # <- Can access topic_arn property

使用ref属性。来自 CloudFormation 文档:

When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the topic ARN, for example: arn:aws:sns:us-east-1:123456789012:mystack-mytopic-NZJ5JSMVGFIE.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html

topic_arn = topic.ref

仅供参考 - 这适用于大多数(如果不是全部)L1 结构。