如何使用boto3通过另一个帐户的SQS订阅一个帐户的SNS主题? [英] How to subscribe an SNS topic of one account by SQS of another account using boto3?

查看:123
本文介绍了如何使用boto3通过另一个帐户的SQS订阅一个帐户的SNS主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个帐户中创建一个SNS主题,并将其附加到配置规则"中. 我有3个这样的帐户,并希望在每个帐户中创建SNS主题. 现在,我想通过第四个帐户的SQS订阅3个不同帐户的所有3个主题.

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.

我能够手动完成.有人可以告诉我如何通过boto3做到这一点.

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

在此先感谢.

推荐答案

要使用boto3通过帐户B中存在的SQS订阅帐户A中存在的SNS主题,请执行以下步骤.

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

在帐户A中,创建SNS主题并添加适当的权限. 例如,

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"
                ]
            )

现在要从帐户B订阅创建的主题,请从帐户B执行以下代码.

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"
            )

现在您将看到帐户B订阅了帐户A的SNS主题.

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

这篇关于如何使用boto3通过另一个帐户的SQS订阅一个帐户的SNS主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆