我正在尝试在一个帐户中创建一个SNS主题,并将其附加到配置规则。 我有3个这样的帐户,并希望在每个帐户中创建SNS主题。 现在我想通过第四个帐户的SQS订阅3个不同帐户的所有3个主题。
我可以手动完成。有人可以告诉我如何通过boto3完成。
先谢谢。
答案 0 :(得分:0)
为了使用boto3通过账户B中的SQS订阅账户A中的SNS主题,以下是该程序。
在帐户A中,创建SNS主题并添加适当的权限。 例如,
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执行以下代码。
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"
)
现在您将看到帐户A的SNS主题已被帐户B订阅。