Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe an SNS topic of one account by SQS of another account using boto3?

I'm trying to create an SNS topic in one account and attach it to Config Rules. I have 3 such accounts and want to create SNS topic in each of the account. Now i want to subscribe all of the 3 topics of 3 different accounts by SQS of the fourth account.

I'm able to do it manually. Can somebody please tell me how it can be done via boto3.

Thanks in Advance.

like image 930
Vinay Wadagavi Avatar asked Oct 11 '17 06:10

Vinay Wadagavi


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.

Can you subscribe an SNS topic to another SNS topic?

No. Amazon SNS is not an available subscription type. You could subscribe an AWS Lambda function and that Lambda function could send messages to desired SNS topics.

How can I fanout identical messages to multiple SQS queues?

The typical way to fanout messages to multiple sqs queues is to use SNS. The s3 event notifications would goto SNS instead of SQS and the SNS would be responsible for fanning those messages out to as many queues as you want.


1 Answers

In order to subscribe a SNS topic present in Account A by an SQS present in Account B using boto3, following is the procedure.

In Account A, create SNS topic and add the proper permission. For example,

import boto3
sns_client = boto3.clien('sns')
topics = sns_client.create_topic(Name='SNS topic name')
sns_client.add_permission(
                TopicArn=str(topics['TopicArn']),
                Label=label,
                AWSAccountId=[
                    "AccountB_Id",
                ],
                ActionName=[
                    "GetTopicAttributes",
                    "SetTopicAttributes",
                    "AddPermission",
                    "RemovePermission",
                    "DeleteTopic",
                    "Subscribe",
                    "ListSubscriptionsByTopic",
                    "Publish",
                    "Receive"
                ]
            )

Now to subscribe the created topic from Account B, execute the following code from account B.

import boto3
subscription_client = boto3.client('sns')
subscription_client.subscribe(
                TopicArn="ARN of the topic created",
                Protocol="sqs",
                Endpoint="ARN of the SQS present in Account B"
            )

Now you would see the SNS topic of account A been subscribed by account B.

like image 175
Vinay Wadagavi Avatar answered Sep 30 '22 19:09

Vinay Wadagavi