Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe a sqs queue to a sns topic that is in a different account, using aws cdk(typescript)

I would like to connect an sqs queue to an sns topic that is in a different account, using cdk (typescript). Below is the code (this code is in a stack) that I think should work but I have some doubts listed below the code (I have not deployed this yet, still trying to learn how to do this first).

    const topic = Topic.fromTopicArn(
      this,
      `${stackName}-topic`,
      `arn:aws:sns:${region}:${accountno}:SubscriptionChanges`
    );

    topic.addSubscription(
      new SqsSubscription(queue, {
        filterPolicy: {
          type: SubscriptionFilter.stringFilter({
            whitelist: [
              'filter1',
            ],
          })
        },
      })
    );
  }
  • I use fromTopicArn to initiate the topic construct. Am I allowed to do this if I am not the owner of the topic (the topic is defined in a different account so I am trying to do this cross account)?
  • Is there a way to create a sqs subscription without creating the topic variable on the first line above?

I have read the documentation, and, there is example code for this, but it only shows how to do this within the same account. Anyone with any experience of this?

like image 929
Bashar Mengana Avatar asked Jan 27 '20 10:01

Bashar Mengana


People also ask

Can SQS subscribe to SNS in different account?

Sending Amazon SNS messages to an Amazon SQS queue in a different account. You can publish a notification to an Amazon SNS topic with one or more subscriptions to Amazon SQS queues in another account.

How do SNS and SQS work together?

When you subscribe an Amazon SQS queue to an Amazon SNS topic, you can publish a message to the topic and Amazon SNS sends an Amazon SQS message to the subscribed queue. The Amazon SQS message contains the subject and message that were published to the topic along with metadata about the message in a JSON document.

Can SQS Cross region?

Amazon SNS supports the cross-region delivery of notifications to Amazon SQS queues and to AWS Lambda functions. When one of the Regions is an opt-in Region, you must specify a different Amazon SNS service principal in the subscribed resource's policy.


1 Answers

So after some research I have some answers.

You are allowed to create a topic construct even if you down own the topic, and you can connect a queue to it, but you (or more specifically, your account number) have to be granted access by the topic owner.

const queue = make_my_queue();
const topic = sns.Topic.fromTopicArn(
  this, // assuming `this` is your Deployment Stack object.
  "myTopicId",
  "arn:aws:sns:eu-west-1:123123123123:MyFriendsGreatSnsTopic");

topic.addSubscription(new snsSubs.SqsSubscription(queue), {
   rawMessageDelivery: true // or false if you want
});
like image 67
Bashar Mengana Avatar answered Oct 18 '22 04:10

Bashar Mengana