本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon 資源型政策 EventBridge
在中執行規則時 EventBridge,會呼叫與規則相關聯的所有目標。規則可以叫用 AWS Lambda 函數、發佈到 Amazon SNS 主題,或將事件轉送到 Kinesis 串流。若要對您擁有的資源進行API呼叫, EventBridge 需要適當的權限。對於 Lambda、Amazon SNS SQS、Amazon 和 Amazon CloudWatch 日誌資源, EventBridge 使用以資源為基礎的政策。對於 Kinesis 串流, EventBridge 會使用以身分識別為基礎的原則。
您可 AWS CLI 以使用將權限新增至目標。若要取得有關如何安裝和配置的資訊 AWS CLI,請參閱《AWS Command Line Interface 使用者指南》 AWS Command Line Interface中的〈使用〉的〈進行設定〉。
Amazon API 閘道許可
若要使用 EventBridge 規則叫用 Amazon API 閘道端點,請將下列權限新增至API閘道端點的政策。
{ "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 記錄檔權限
當 CloudWatch 記錄檔是規則的目標時, EventBridge 會建立記錄資料流,而「 CloudWatch 記錄檔」會將事件中的文字儲存為記錄項目。若 EventBridge 要允許建立記錄資料流並記錄事件, CloudWatch 記錄檔必須包含可 EventBridge 寫入記 CloudWatch 錄檔的資源型政策。
如果您使用將 CloudWatch 記錄檔新增為規則的目標,則會自動建立以資源為基礎的策略。 AWS Management Console 如果您使用 AWS 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 記錄API參考指南PutResourcePolicy中的。
AWS Lambda 權限
若要使用 EventBridge 規則叫用 AWS 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 用 Lambda 函數的權限 AWS CLI
-
在命令提示中,輸入以下命令:
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
"
如需設定允許叫用 Lambda 函數的權限的詳細資訊,請參閱AWS Lambda 開發人員指南中的AddPermission並 EventBridge 將 Lambda 與排程事件搭配使用。
Amazon SNS 許可
若 EventBridge 要允許發佈到 Amazon SNS 主題,請使用aws sns
get-topic-attributes
和命aws sns set-topic-attributes
令。
注意
您不能在 Amazon SNS 主題政策中使用Condition
塊 EventBridge.
若要新增可 EventBridge 以發佈SNS主題的權限
-
若要列出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" } } -
使用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
\"} -
將您在上一個步驟中建立的字串新增至
"Policy"
屬性內的"Statement"
集合中。 -
使用
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 簡單通知服務API參考中的SetTopicAttributes動作。
Amazon SQS 許可
若要允許 EventBridge 規則叫用 Amazon SQS 佇列,請使用aws sqs
get-queue-attributes
和aws sqs set-queue-attributes
命令。
如果SQS佇列的原則是空的,您必須先建立原則,然後才能將權限陳述式新增至其中。新SQS佇列具有空白原則。
如果SQS佇列已有原則,您需要複製原始原則,並將其與新陳述式結合,才能將權限陳述式新增至其中。
新增允許 EventBridge 規則呼叫SQS佇列的權限
-
SQS列出佇列屬性。在命令提示中,輸入以下命令:
aws sqs get-queue-attributes \ --queue-url https://sqs.
region
.amazonaws.com/account-id
/queue-name
\ --attribute-names Policy -
添加以下陳述式。
{ "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
" } } } -
使用字符串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
\"}} -
建立稱為
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
\"}}}]}" } -
使用剛建立的
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 簡單佇列服務開發人員指南中的 Amazon SQS 政策範例。
EventBridge 管道細節
EventBridge 管道不支援以資源為基礎的政策,也沒有支援APIs以資源為基礎的政策條件。
但是,如果您透過介面VPC端點設定管道存取,則該VPC端點支援可讓您管理 EventBridge Pipe 存取權的資源策略APIs。如需詳細資訊,請參閱 將 Amazon EventBridge 與界面VPC端點搭配使用