AWS CLI:何时需要向 SNS 主题添加权限?

AWS CLI: When is it necessary to add permission to a SNS topic?

我正在了解 AWS;我正在了解的特定用例是将对象添加到 S3 存储桶会触发 SNS 通知,该通知已被 Lambda 订阅,从而触发 Lambda.

在线阅读让我找到了s3api put-bucket-notification-configuration page,上面写着

The SNS topic must have an IAM policy attached to it that allows Amazon S3 to publish to it

这让我找到了 sns add-permission page,其请求签名是:

  add-permission
--topic-arn <value>
--label <value>
--aws-account-id <value>
--action-name <value>

问题:即使发布者与创建该主题的帐户相同,是否有必要显式添加对 SNS 主题的权限?
链接文档中的措辞暗示只有当发布者来自不同的帐户时才有必要,但我不确定我是否正确解释了这一点。

例如在我的实验中,我使用的所有东西都是同一个帐户的一部分。例如:

$ aws sns list-topics --profile=admin --endpoint-url=http://localhost:4575
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:us-east-1:000000000000:my-test-topic"
        }
    ]
}
$
$ aws --profile=lambda-admin --endpoint-url=http://localhost:4574 lambda list-functions
{
    "Functions": [
        {
            "FunctionName": "first_lambda",
            "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:first_lambda",
            "Runtime": "python3.7",
            "Role": "arn:aws:iam::000000000000:role/lambda-role",
            "Handler": "first_lambda.lambda_handler",
            "CodeSize": 311,
            "Description": "",
            "Timeout": 5,
            "LastModified": "2020-06-16T05:10:16.311+0000",
            "CodeSha256": "jRcHzt34ZSDUCyx+INftvu14njRqGeSozKa0Uxv4J98=",
            "Version": "$LATEST",
            "TracingConfig": {
                "Mode": "PassThrough"
            },
            "RevisionId": "af64db69-0b5a-41ad-86c2-8467a60cf618",
            "State": "Active"
        }
    ]
}

(我还没有找到从 CLI 获取 S3 存储桶的 ARN 的方法,但我的存储桶与同一个帐户相关联)。

Question: is it necessary to explicitly add-permission to a SNS topic even if the publisher is in the same account in which the topic was created?

sqs 的默认策略包含以下内容:

      "Principal": {
        "AWS": "*"
      }

这将允许来自您的帐户任何 IAM 实体(IAM 用户、角色)(Condition;未显示,但出现在政策中)到 SNS:Publish(除其他事项外)到您的主题。不过,他们仍然必须拥有自己的发布权限。比如对于lambda你还需要给它的executions角色加上sns权限,即confuses people.

需要注意的重要一点是 AWSPrincipal 中的 AWS 键不包括服务,例如 S3。原因是 S3 服务的 Principal 看起来像这样:

   "Principal": {
     "Service": "s3.amazonaws.com"  
   }

因此您必须 explicitly 允许服务 S3 在主题策略中发布到您的主题。

I haven't found a way to get the ARN of a S3 bucket from the CLI, but mine is associated with the same account

存储桶 ARN 已知 format:

arn:aws:s3:::bucket_name

或在中国:

arn:aws-cn:s3:::bucket_name

因此,即使 CLI 没有明确提供 ARN,您也可以很容易地自行构建它。如果您不确定您的存储桶是否在中国,那么您可以使用 get-bucket-location 来验证。