针对 Amazon EventBridge 使用基于资源的策略 - Amazon EventBridge
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

针对 Amazon EventBridge 使用基于资源的策略

规则在 EventBridge 中运行时,将调用与该规则关联的所有目标。规则可以调用 Amazon Lambda 函数、发布到 Amazon SNS 主题或将事件中继到 Kinesis 流。为了对您拥有的资源执行 API 调用,EventBridge 需要相应权限。对于 Lambda、Amazon SNS、Amazon SQS 和 Amazon CloudWatch Logs 资源,EventBridge 使用基于资源的策略。对于 Kinesis 流,EventBridge 使用基于身份的策略。

您可以使用 Amazon CLI 向目标添加权限。有关如何安装和配置 Amazon CLI 的信息,请参阅 Amazon Command Line Interface 用户指南中的使用 Amazon Command Line Interface 进行设置

Amazon API Gateway 权限

要使用 EventBridge 规则调用您的 Amazon API Gateway 端点,请在 API Gateway 端点的策略中添加以下权限。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "execute-api:Invoke", "Condition": { "ArnEquals": { "aws:SourceArn": "arn:aws:events:region:account-id:rule/rule-name" } }, "Resource": [ "execute-api:/stage/GET/api" ] } ] }

CloudWatch Logs 权限

如果 CloudWatch Logs 是规则的目标,EventBridge 会创建日志流,并且 CloudWatch Logs 会将事件中的文本存储为日志条目。要允许 EventBridge 创建日志流并记录事件,CloudWatch Logs 必须包含基于资源的策略,允许 EventBridge 对 CloudWatch Logs 进行写入。

如果您使用 Amazon Web Services Management Console将 CloudWatch Logs 作为规则目标添加,则会自动创建基于资源的策略。如果您使用 Amazon CLI 添加目标,并且策略尚不存在,则必须创建此策略。

以下示例允许 EventBridge 写入名称以 /aws/events/ 开头的所有日志组。如果您对这些类型的日志使用其他命名策略,请对示例进行相应调整。

{ "Statement": [ { "Action": [ "logs:CreateLogStream", "logs:PutLogEvents" ], "Effect": "Allow", "Principal": { "Service": ["events.amazonaws.com", "delivery.logs.amazonaws.com"] }, "Resource": "arn:aws:logs:region:account:log-group:/aws/events/*:*", "Sid": "TrustEventsToStoreLogEvent" } ], "Version": "2012-10-17" }

有关更多信息,请参阅《CloudWatch Logs API 参考指南》中的 PutResourcePolicy

Amazon Lambda 权限

要使用 EventBridge 规则调用您的 Amazon Lambda 函数,可将以下权限添加到您的 Lambda 函数的策略中。

{ "Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:region:account-id:function:function-name", "Principal": { "Service": "events.amazonaws.com" }, "Condition": { "ArnLike": { "AWS:SourceArn": "arn:aws:events:region:account-id:rule/rule-name" } }, "Sid": "InvokeLambdaFunction" }
要添加以上权限,允许 EventBridge 使用 Amazon CLI 调用 Lambda 函数
  • 在命令提示符处,输入以下命令:

    aws lambda add-permission --statement-id "InvokeLambdaFunction" \ --action "lambda:InvokeFunction" \ --principal "events.amazonaws.com" \ --function-name "arn:aws:lambda:region:account-id:function:function-name" \ --source-arn "arn:aws:events:region:account-id:rule/rule-name"

有关如何设置权限,以使 EventBridge 能够调用 Lambda 函数的更多信息,请参阅《Amazon Lambda 开发人员指南》中的 AddPermission将 Lambda 与计划事件配合使用

Amazon SNS 权限

要允许 EventBridge 发布到 Amazon SNS 主题,请使用 aws sns get-topic-attributesaws sns set-topic-attributes 命令。

注意

您不能在适用于 EventBridge 的 Amazon SNS 主题策略中使用 Condition 块。

添加权限,允许 EventBridge 发布 SNS 主题
  1. 要列出某个 SNS 主题的属性,请使用以下命令。

    aws sns get-topic-attributes --topic-arn "arn:aws:sns:region:account-id:topic-name"

    以下示例显示一个新 SNS 主题的结果。

    { "Attributes": { "SubscriptionsConfirmed": "0", "DisplayName": "", "SubscriptionsDeleted": "0", "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", "Owner": "account-id", "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\"],\"Resource\":\"arn:aws:sns:region:account-id:topic-name\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"account-id\"}}}]}", "TopicArn": "arn:aws:sns:region:account-id:topic-name", "SubscriptionsPending": "0" } }
  2. 使用 JSON 转字符串转换器,将以下语句转换为字符串。

    { "Sid": "PublishEventsToMyTopic", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sns:Publish", "Resource": "arn:aws:sns:region:account-id:topic-name" }

    将语句转换为字符串后,如以下示例所示。

    {\"Sid\":\"PublishEventsToMyTopic\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":\"sns:Publish\",\"Resource\":\"arn:aws:sns:region:account-id:topic-name\"}
  3. 将您在上一步中创建的字符串添加到 "Policy" 属性中的 "Statement" 集合中。

  4. 使用 aws sns set-topic-attributes 命令设置新策略。

    aws sns set-topic-attributes --topic-arn "arn:aws:sns:region:account-id:topic-name" \ --attribute-name Policy \ --attribute-value "{\"Version\":\"2012-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\"],\"Resource\":\"arn:aws:sns:region:account-id:topic-name\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"account-id\"}}}, {\"Sid\":\"PublishEventsToMyTopic\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":\"sns:Publish\",\"Resource\":\"arn:aws:sns:region:account-id:topic-name\"}]}"

有关更多信息,请参阅《Amazon Simple Notification Service API 参考》中的 SetTopicAttributes

Amazon SQS 权限

要允许 EventBridge 规则调用 Amazon SQS 队列,请使用 aws sqs get-queue-attributesaws sqs set-queue-attributes 命令。

如果 SQS 队列的策略为空,您首先需要创建一个策略,然后可以向其中添加权限语句。新 SQS 队列的策略为空。

如果 SQS 队列已有策略,您需要复制原始策略,并将其与新语句组合,向其中添加权限语句。

添加权限,允许 EventBridge 规则调用 SQS 队列
  1. 列出 SQS 队列属性。在命令提示符处,输入以下命令:

    aws sqs get-queue-attributes \ --queue-url https://sqs.region.amazonaws.com/account-id/queue-name \ --attribute-names Policy
  2. 添加以下语句。

    { "Sid": "AWSEvents_custom-eventbus-ack-sqs-rule_dlq_sqs-rule-target", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sqs:SendMessage", "Resource": "arn:aws:sqs:region:account-id:queue-name", "Condition": { "ArnEquals": { "aws:SourceArn": "arn:aws:events:region:account-id:rule/bus-name/rule-name" } } }
  3. 使用 JSON 转字符串转换器,将上一语句转换为字符串。在将策略转换为字符串后,如下所示。

    {\"Sid\": \"EventsToMyQueue\", \"Effect\": \"Allow\", \"Principal\": {\"Service\": \"events.amazonaws.com\"}, \"Action\": \"sqs:SendMessage\", \"Resource\": \"arn:aws:sqs:region:account-id:queue-name\", \"Condition\": {\"ArnEquals\": {\"aws:SourceArn\": \"arn:aws:events:region:account-id:rule/rule-name\"}}
  4. 使用以下内容创建名为 set-queue-attributes.json 的文件。

    { "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"arn:aws:sqs:region:account-id:queue-name/SQSDefaultPolicy\",\"Statement\":[{\"Sid\": \"EventsToMyQueue\", \"Effect\": \"Allow\", \"Principal\": {\"Service\": \"events.amazonaws.com\"}, \"Action\": \"sqs:SendMessage\", \"Resource\": \"arn:aws:sqs:region:account-id:queue-name\", \"Condition\": {\"ArnEquals\": {\"aws:SourceArn\": \"arn:aws:events:region:account-id:rule/rule-name\"}}}]}" }
  5. 使用您刚刚创建的 set-queue-attributes.json 文件作为输入,设置策略属性,如以下命令所示。

    aws sqs set-queue-attributes \ --queue-url https://sqs.region.amazonaws.com/account-id/queue-name \ --attributes file://set-queue-attributes.json

有关更多信息,请参阅《Amazon Simple Queue Service 开发人员指南》中的 Amazon SQS 策略示例

EventBridge Pipes 具体信息

EventBridge Pipes 不支持基于资源的策略,也没有 API 支持基于资源的策略条件。