AWS 服务的 Amazon EventBridge 策略作为使用 CF/SAM 的目标

Amazon EventBridge Policies for AWS Services as targets using CF/SAM

我正在使用 AWS CloudFormation 设置 EventBridge 总线 + 规则 + 目标(比如 SNS)。对于作为目标的 SNS,根据 https://docs.aws.amazon.com/eventbridge/latest/userguide/resource-based-policies-eventbridge.html#sns-permissions 上的文档,我需要在 CloudFormation 之外应用资源策略,我认为 CF 还不支持吗? 对于作为目标的 CW 日志组,我使用 aws logs put-resource-policy 在脚本中进行设置。有没有更好的方法来自动执行此操作?

您提供的 link 指的是为 SNS 主题设置权限。 CloudFormation 通过 AWS::SNS::TopicPolicy.

支持设置此类权限

但是,您还声明要在 CloudWatch Logs 上设置基于资源的策略 (aws logs put-resource-policy)。如果是这种情况,那么您是对的,并且 CloudFormation 不支持它。

您必须使用基于 lambda 函数的 custom resource 才能将此类功能添加到您的模板中。

这是我的 SAM 的片段:

{
  "MyDevQueue": {
    "Properties": {
      "QueueName": "my-dev-queue",
      "ReceiveMessageWaitTimeSeconds": 20,
      "Tags": [
        {
          "Key": "env",
          "Value": "dev"
        }
      ],
      "VisibilityTimeout": 300
    },
    "Type": "AWS::SQS::Queue"
  },
  "MyDevQueuePolicy": {
    "Properties": {
      "PolicyDocument": {
        "Statement": [
          {
            "Action": [
              "SQS:SendMessage"
            ],
            "Condition": {
              "ArnEquals": {
                "aws:SourceArn": "arn:aws:events:<region>:<AccountID>:rule/my-dev-queue/my-dev-queue"
              }
            },
            "Effect": "Allow",
            "Principal": {
              "Service": [
                "events.amazonaws.com"
              ]
            },
            "Resource": [
              {
                "Fn::GetAtt": [
                  "MyDevQueue",
                  "Arn"
                ]
              }
            ]
          }
        ]
      },
      "Queues": [
        "MyDevQueue"
      ]
    },
    "Type": "AWS::SQS::QueuePolicy"
  }
}