Boto3 SNS 发布到通配符主题 ARN

Boto3 SNS Publish to a wildcard Topic ARN

背景

AWS 服务是区域性的(例如 us-west-2us-east-1)并且 boto3 库要求您在访问客户端或资源之前设置默认区域.但是,文档 here 显示您可以使用带有通配符的 SNS 主题 ARN 代替区域。文档说:

文档:亚马逊简单通知服务 (Amazon SNS)

语法:

arn:aws:sns:region:account-id:topicname
arn:aws:sns:region:account-id:topicname:subscriptionid

示例:

arn:aws:sns:*:123456789012:my_corporate_topic
arn:aws:sns:us-east-1:123456789012:my_corporate_topic:02034b43-fefa-4e07-a5eb-3be56f8c54ce

代码

当我使用 boto3 的 SNS Resource/Client 发布到主题 ARN(该区域具有通配符)时,出现以下错误。当我没有该区域的通配符时(例如,我指定 us-west-2),一切正常。我查看了 boto3 库,它似乎只是替换 JSON 映射中的值(例如插入主题字符串),所以我不明白如果上面的文档显示它是有效的,为什么这将是一个无效参数。

import boto3

client = boto3.client('sns', region_name='us-west-2')
client.publish(TopicArn='arn:aws:sns:*:123456789:some-topic', Message='SomeMessage')

错误信息

File "/Users/wliu/.virtualenvs/myenv/lib/python2.7/site-packages/botocore/client.py", line 548, in _make_api_call
raise ClientError(parsed_response, operation_name)
ClientError: An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: TopicArn Reason: A * ARN must begin with arn:null, not arn:aws:sns:*:123456789:my_topic

该文档并未表明它对您使用它的上下文有效。您误用或误解了文档,混淆了模式和文字的适用性。发布需要文字,并且在基础 API.

文档的 relevant section 中没有提及通配符

You can use wildcards as part of the resource ARN 当指定 IAM 策略声明适用的资源时,当特定服务支持资源级策略时。

来自特定于 SNS 的策略语言文档:

For Amazon SNS, topics are the only resource type you can specify in a policy. Following is the Amazon Resource Name (ARN) format for topics.

Example

If you had a topic named my_topic in each of the different Regions that Amazon SNS supports, you could specify the topics with the following ARN.

arn:aws:sns:*:123456789012:my_topic

http://docs.aws.amazon.com/sns/latest/dg/UsingIAMwithSNS.html#SNS_ARN_Format

然而,这仅适用于策略,它也支持 arn:aws:sns:*:123456789012:bob_* 等模式,并且这种模式(可能更直观)不是 Publish 请求的有效主题。