Matthew
Matthew

Reputation: 867

Referencing SNS Topic that already exists in CDK?

I have an SNS topic that already exists in our AWS environment. Instead of creating a new SNS topic, I want to simply reference the already-existing topic and publish a message to it.

Right now, I have the following:

const topic = new sns.Topic(this, 'AggregateSNS', {

      contentBasedDeduplication: false,
      displayName: 'Customer subscription topic',
      fifo: true,
      topicName: 'MySNSTopic',

      //how to reference existing topics?
    });

My understanding is this will create a new SNS topic, but as I said above, I want to reference an existing SNS topic. How do I do I reference an existing SNS topic in CDK?

Upvotes: 2

Views: 6572

Answers (1)

fedonev
fedonev

Reputation: 25739

You get a read-only reference to an existing SNS topic with the static fromTopicArn method. A common reason to do this is adding an existing topic as an event source.

const topic = sns.Topic.fromTopicArn(this, 'MyTopic', <topic-arn>);

func.addEventSource(new sources.SnsEventSource(topic));

If you need use the publish API from your CDK app (to send a notification upon certain deploy lifecycle events, for instance), you would use a custom resource. The AwsCustomResource construct makes it easy to make SDK calls as part of the deploy process.

Upvotes: 5

Related Questions