2017-10-11 117 views
4

我試圖在一個帳戶中創建一個SNS主題並將其附加到配置規則。 我有3個這樣的帳戶,並希望在每個帳戶中創建SNS主題。 現在我想通過第四個賬戶的SQS訂閱3個不同賬戶的所有3個主題。如何使用boto3通過另一個帳戶的SQS訂閱一個帳戶的SNS主題?

我可以手動完成。有人可以告訴我如何通過boto3完成。

在此先感謝。

+0

到目前爲止,您對http://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.subscribe – mootmoot

+0

已嘗試了什麼?當您手動執行該操作時,您採取了哪些步驟? – Mangohero1

+0

我已經提到aws文檔[SendMessageToSQS](http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.cross.account.html) –

回答

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.

相關問題