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

爷,独闯天下 提交于 2019-12-04 07:05:13

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!