Search code examples
pythonamazon-web-servicesamazon-snsaws-cdk

CfnTopic Python AWS CDK access Topic ARN?


Given a CfnTopic using the AWS CDK for Python, how does one access the Topic ARN attribute? I'm able to access this property using the higher level Topic construct, but not the lower level CfnTopic construct.

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

Solution

  • Use the ref property. From the CloudFormation docs:

    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
    

    FYI - this applies to most (if not all) L1 constructs.