2016-05-05 53 views
0

我想在代码中动态创建Amazon SNS主题。我正在使用适用于iOS的AWS Mobile Hub sdk。如何在代码中创建AWS SNS主题(iOS Mobile Hub SDK)

当我尝试创建一个话题

… 
AWSSNSCreateTopicInput* input = [AWSSNSCreateTopicInput new]; 
NSString* name = @"topic_name"; 
[input setName:name]; 

[[[[AWSSNS defaultSNS] createTopic:input] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSSNSCreateTopicResponse *> * _Nonnull task) 
… 

我得到一个错误的AWS:

<Message>User: (role/credentials) is not authorized to perform: SNS:CreateTopic on resource: (topic)</Message> 

(角色/凭证)表示IAM作用及其Cognito凭据。 (主题)是我通过给主题名称要求主题

AWS移动中心创建了下面的推送策略为我的手机枢纽角色的ARN:

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "sns:CreatePlatformEndpoint", 
       "sns:GetEndpointAttributes", 
       "sns:SetEndpointAttributes" 
      ], 
      "Resource": [ 
       "(APN role arn)" 
      ] 
     }, 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "sns:Subscribe", 
       "sns:Publish", 
       "sns:Unsubscribe" 
      ], 
      "Resource": [ 
       "(dynamodb role arn)", 
       "(Mobile Hub Role arn)" 
      ] 
     }, 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "sns:ListTopics" 
      ], 
      "Resource": [ 
       "*" 
      ] 
     } 
    ] 
} 

我尝试添加该行

"sns:CreateTopic", 

中间设置的权限(刚刚在“sns:订阅”上面),但没有解决错误。从错误消息中阅读AWS文档,似乎必须为每个创建的主题添加一个策略才能使用它。下面是从AWS文档,可能是相关的2段:

The following example shows the permissions that are automatically created by AWS Config for a new topic. This policy statement allows AWS Config to publish to a specified Amazon SNS topic. 

If you want to use an existing SNS topic from another account or you set up your delivery channel using the API, make sure to attach the following policy to the SNS topic. 

{ 
    "Id": "Policy1415489375392", 
    "Statement": [ 
    { 
     "Sid": "AWSConfigSNSPolicy20150201", 
     "Action": [ 
     "SNS:Publish" 
     ], 
     "Effect": "Allow", 
     "Resource": "arn:aws:sns:region:account-id:myTopic", 
     "Principal": { 
     "Service": [ 
      "config.amazonaws.com" 
     ] 
     } 
    } 
    ] 
} 

IAM Role Policy for Amazon SNS Topic 

Use this example policy as a model for granting AWS Config permissions to access your SNS topic: 

{ 
    "Version": "2012-10-17", 
    "Statement": 
    [ 
    { 
     "Effect":"Allow", 
     "Action":"sns:Publish", 
     "Resource":"yourSNStopicARN" 
    } 
    ] 
} 

这一切我已经能够找到的有关使用SDK创建主题。任何人都可以提供或指出我一个完整的例子吗?

感谢

回答

0

的AWS论坛亚马逊SNS(简单通知服务),该服务支持移动推,可能是一个更好的地方,以获得关于这一主题的帮助。
https://forums.aws.amazon.com/forum.jspa?forumID=72

问题似乎是适当的移动应用用户的IAM角色没有权限创建主题。移动中心不会为移动应用用户提供默认创建SNS主题的权限。您应该添加sns:CreateTopic权限到具有sns的语句:ListTopic,像这样...

{ 
     "Effect": "Allow", 
     "Action": [ 
      "sns:ListTopics", 
      "sns:CreateTopic", 
     ], 
     "Resource": [ 
      "*" 
     ] 
    } 
+0

Andrew C感谢您的回复。我做了你提出的改变,我仍然得到同样的错误。我会在你建议的论坛上重新发布这个问题。 –

+0

那么,我会在AWS上发帖,但即使我有一个不是新的AWS开发人员帐户,但我已登录,但未经授权将新线程发布到AWS论坛。我还没有想出如何获得邮件许可。 –

+0

今天早上新鲜的眼睛向我展示了我在同一个地方得到了不同的错误。我还必须将sns:SetTopicAttributes添加到策略的同一部分。感谢您的帮助,我为我的草率结论表示歉意。 –