为了使用 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 订阅。