入門:建立 Amazon EventBridge 事件匯流排規則 - Amazon EventBridge

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

入門:建立 Amazon EventBridge 事件匯流排規則

為了熟悉 EventBridge 規則及其功能,我們將使用 AWS CloudFormation 範本來設定事件匯流排規則和相關聯的元件,包括事件來源、事件模式和目標。然後,我們可以探索規則如何運作以選取您想要的事件。

範本會在預設事件匯流排上建立規則。此規則使用事件模式來篩選來自特定 Amazon S3 儲存貯體的事件。規則會將相符事件傳送至指定的目標,即 Amazon SNS 主題。每次在儲存貯體中建立物件時,規則都會傳送通知至 主題,然後傳送電子郵件至您指定的電子郵件地址。

部署的資源包含:

  • 啟用 EventBridge 通知以做為事件來源的 Amazon S3 儲存貯體。

  • 作為通知目標的 Amazon SNS 主題和電子郵件訂閱。

  • 授予 EventBridge 發佈至 Amazon SNS 主題必要許可的執行角色。

  • 規則本身,其:

    • 定義僅符合特定 Amazon S3 儲存貯體Object Created事件的事件模式。

    • 將 Amazon SNS 主題指定為 EventBridge 交付相符事件的目標。

如需範本的特定技術詳細資訊,請參閱 範本詳細資訊

Amazon S3 事件會與規則的事件模式相符,如果相符,則會傳送至 SNS 主題。

開始之前

若要在 EventBridge 中接收 Amazon S3 事件,您必須在 Amazon S3 中啟用 EventBridge。本主題假設 EventBridge 已啟用。如需詳細資訊,請參閱《Amazon S3 使用者指南》中的啟用 EventBridge

使用 CloudFormation 建立規則

若要建立規則及其相關資源,我們將建立 CloudFormation 範本,並使用它來建立包含範例規則的堆疊,並完整包含來源和目標。

重要

如果您從此範本建立堆疊,則會向您收取所使用的 Amazon 資源費用。

首先,建立 CloudFormation 範本。

  1. 範本區段中,按一下 JSONYAML 標籤上的複製圖示,以複製範本內容。

  2. 將範本內容貼到新檔案中。

  3. 在本機儲存檔案。

接著,使用您儲存的範本來佈建 CloudFormation 堆疊。

使用 CloudFormation 建立堆疊 (主控台)
  1. 開啟位在 https://console.aws.amazon.com/cloudformation/ 的 CloudFormation​ 主控台。

  2. 堆疊頁面上,從建立堆疊功能表中,選擇新資源 (標準)

  3. 指定範本:

    1. 先決條件下,選擇選擇現有範本

    2. 指定範本下,選擇上傳範本檔案

    3. 選擇選擇檔案,導覽至範本檔案,然後選擇它。

    4. 選擇下一步

  4. 指定堆疊詳細資訊:

    1. 輸入堆疊名稱。

    2. 對於參數,請接受 BucketNameSNSTopicDisplayNameSNSTopicNameRuleName 的預設值,或輸入您自己的預設值。

    3. EmailAddress 中,輸入您要接收通知的有效電子郵件地址。

    4. 選擇下一步

  5. 設定堆疊選項:

    1. 堆疊失敗選項下,選擇刪除所有新建立的資源

      注意

      選擇此選項可避免您可能因為刪除政策指定資源保留這些資源,即使堆疊建立失敗也一樣。如需詳細資訊,請參閱 CloudFormation 使用者指南中的 DeletionPolicy 屬性

    2. 接受所有其他預設值。

    3. 功能下,勾選核取方塊以確認 CloudFormation 可能會在您的帳戶中建立 IAM 資源。

    4. 選擇下一步

  6. 檢閱堆疊詳細資訊,然後選擇提交

使用 CloudFormation 建立堆疊 (AWS CLI)

您也可以使用 AWS CLI 來建立堆疊。

  • 使用 create-stack 命令。

    • 接受預設範本參數值,指定堆疊名稱和您的電子郵件地址。使用 template-body 參數傳遞範本內容,或template-url指定 URL 位置。

      aws cloudformation create-stack \ --stack-name eventbridge-rule-tutorial \ --template-body template-contents \ --parameters ParameterKey=EmailAddress,ParameterValue=your.email@example.com \ --capabilities CAPABILITY_IAM
    • 覆寫一或多個範本參數的預設值 (s)。例如:

      aws cloudformation create-stack \ --stack-name eventbridge-rule-tutorial \ ----template-body template-contents \ --parameters \ ParameterKey=EmailAddress,ParameterValue=your.email@example.com \ ParameterKey=BucketName,ParameterValue=my-custom-bucket-name \ ParameterKey=RuleName,ParameterValue=my-custom-rule-name \ --capabilities CAPABILITY_IAM

CloudFormation 會建立堆疊。堆疊建立完成後,堆疊資源即可使用。您可以使用堆疊詳細資訊頁面上的資源索引標籤來檢視帳戶中佈建的資源。

建立堆疊後,您會在提供的地址收到訂閱確認電子郵件。您必須確認此訂閱才能接收通知。

探索規則功能

建立規則後,您可以使用 EventBridge 主控台來觀察規則操作和測試事件交付。

  1. 開啟位於 https://https://console.aws.amazon.com/events/home?#/rules 的 EventBridge 主控台。

  2. 選擇您建立的規則。

    在規則詳細資訊頁面上,規則詳細資訊區段會顯示規則的相關資訊,包括其事件模式和目標。

檢查事件模式

在我們測試規則操作之前,讓我們來檢查我們指定的事件模式,以控制哪些事件會傳送到目標。規則只會將符合模式條件的事件傳送至目標。在此情況下,我們只想要 Amazon S3 在特定儲存貯體中建立物件時產生的事件。

  • 在規則詳細資訊頁面的事件模式下,您可以看到事件模式只會選取下列事件:

    • 來源是 Amazon S3 服務 (aws.s3)

    • detail-type 是 Object Created

    • 儲存貯體名稱符合我們建立的儲存貯體名稱

    { "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["eventbridge-rule-example-source"] } } }

透過規則傳送事件

接下來,我們將在事件來源中產生事件,以測試規則比對和交付是否正常運作。為此,我們會將物件上傳至指定為事件來源的 S3 儲存貯體。

  1. 開啟位於 https://console.aws.amazon.com/s3/ 的 Amazon S3 主控台。

  2. 儲存貯體清單中,選擇您使用範本建立的儲存貯體 (預設名稱:eventbridge-rule-example-source)。

  3. 選擇上傳

  4. 上傳測試檔案以產生Object Created事件:

    1. 選擇新增檔案,然後從您的電腦中選取檔案。

    2. 選擇上傳

  5. 等待一段時間,讓 EventBridge 處理事件並傳送通知。

  6. 請檢查您的電子郵件以取得物件建立事件的通知。電子郵件將包含 S3 事件的詳細資訊,包括儲存貯體名稱和物件金鑰。

檢視規則指標

您可以檢視規則的指標,以確認事件處理正確。

  1. EventBridge 主控台中,選擇您的規則。

  2. 選擇指標標籤。

  3. 您可以檢視指標,例如:

    • 調用:觸發規則的次數。

    • TriggeredRules:相符事件觸發的規則數目。

清除:刪除資源

最後一步,我們將刪除堆疊及其包含的資源。

重要

只要堆疊中包含的 Amazon 資源存在,就會向您收取費用。

  1. 開啟位在 https://console.aws.amazon.com/cloudformation/ 的 CloudFormation​ 主控台。

  2. 堆疊頁面上,選擇從範本建立的堆疊,然後選擇刪除,然後確認刪除

    CloudFormation 會啟動刪除堆疊及其包含的所有資源。

CloudFormation 範本詳細資訊

此範本會在您的帳戶中建立資源並授予許可。

資源

本教學課程的 CloudFormation 範本會在您的帳戶中建立下列資源:

重要

如果您從此範本建立堆疊,則會向您收取所使用的 Amazon 資源費用。

  • AWS::S3::Bucket:Amazon S3 儲存貯體,可做為規則的事件來源,並啟用 EventBridge 通知。

  • AWS::SNS::Topic:Amazon SNS 主題,做為規則相符事件的目標。

  • AWS::SNS::Subscription:SNS 主題的電子郵件訂閱。

  • AWS::IAM::Role:IAM 執行角色將許可授予您帳戶中的 EventBridge 服務。

  • AWS::Events::Rule:將 Amazon S3 儲存貯體事件連線至 Amazon SNS 主題的規則。

許可

範本包含代表執行角色AWS::IAM::Role的資源。此角色會授予 EventBridge 服務 (events.amazonaws.com) 您帳戶中的下列許可。

下列許可是透過 受管政策 授予AmazonSNSFullAccess

  • 完整存取 Amazon SNS 資源和操作

CloudFormation 範本

將下列 YAML 程式碼儲存為個別檔案,以做為本教學課程的 CloudFormation 範本。

YAML
AWSTemplateFormatVersion: '2010-09-09' Description: '[AWSDocs] EventBridge: event-bus-rule-get-started' Parameters: BucketName: Type: String Description: Name of the S3 bucket Default: eventbridge-rule-example-source SNSTopicDisplayName: Type: String Description: Display name for the SNS topic Default: eventbridge-rule-example-target SNSTopicName: Type: String Description: Name for the SNS topic Default: eventbridge-rule-example-target RuleName: Type: String Description: Name for the EventBridge rule Default: eventbridge-rule-example EmailAddress: Type: String Description: Email address to receive notifications AllowedPattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[a-zA-Z0-9-]*(\\.[a-zA-Z0-9-]+)*$' Resources: # S3 Bucket with notifications enabled S3Bucket: Type: AWS::S3::Bucket Properties: BucketName: !Ref BucketName NotificationConfiguration: EventBridgeConfiguration: EventBridgeEnabled: true # SNS Topic for email notifications SNSTopic: Type: AWS::SNS::Topic Properties: DisplayName: !Ref SNSTopicDisplayName TopicName: !Ref SNSTopicName # SNS Subscription for email SNSSubscription: Type: AWS::SNS::Subscription Properties: Protocol: email Endpoint: !Ref EmailAddress TopicArn: !Ref SNSTopic # EventBridge Rule to match S3 object creation events and send them to the SNS topic EventBridgeRule: Type: AWS::Events::Rule Properties: Name: !Ref RuleName Description: "Rule to detect S3 object creation and send email notification" EventPattern: source: - aws.s3 detail-type: - "Object Created" detail: bucket: name: - !Ref BucketName State: ENABLED Targets: - Id: SendToSNS Arn: !Ref SNSTopic RoleArn: !GetAtt EventBridgeRole.Arn # IAM Role for EventBridge to publish to SNS EventBridgeRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: events.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonSNSFullAccess Outputs: BucketName: Description: Name of the S3 bucket Value: !Ref S3Bucket SNSTopicARN: Description: ARN of the SNS topic Value: !Ref SNSTopic EmailSubscription: Description: Email address for notifications Value: !Ref EmailAddress
JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "[AWSDocs] EventBridge: event-bus-rule-get-started", "Parameters": { "BucketName": { "Type": "String", "Description": "Name of the S3 bucket", "Default": "eventbridge-rule-example-source" }, "SNSTopicDisplayName": { "Type": "String", "Description": "Display name for the SNS topic", "Default": "eventbridge-rule-example-target" }, "SNSTopicName": { "Type": "String", "Description": "Name for the SNS topic", "Default": "eventbridge-rule-example-target" }, "RuleName": { "Type": "String", "Description": "Name for the EventBridge rule", "Default": "eventbridge-rule-example" }, "EmailAddress": { "Type": "String", "Description": "Email address to receive notifications", "AllowedPattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[a-zA-Z0-9-]*(\\.[a-zA-Z0-9-]+)*$" } }, "Resources": { "S3Bucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": { "Ref": "BucketName" }, "NotificationConfiguration": { "EventBridgeConfiguration": { "EventBridgeEnabled": true } } } }, "SNSTopic": { "Type": "AWS::SNS::Topic", "Properties": { "DisplayName": { "Ref": "SNSTopicDisplayName" }, "TopicName": { "Ref": "SNSTopicName" } } }, "SNSSubscription": { "Type": "AWS::SNS::Subscription", "Properties": { "Protocol": "email", "Endpoint": { "Ref": "EmailAddress" }, "TopicArn": { "Ref": "SNSTopic" } } }, "EventBridgeRule": { "Type": "AWS::Events::Rule", "Properties": { "Name": { "Ref": "RuleName" }, "Description": "Rule to detect S3 object creation and send email notification", "EventPattern": { "source": [ "aws.s3" ], "detail-type": [ "Object Created" ], "detail": { "bucket": { "name": [ { "Ref": "BucketName" } ] } } }, "State": "ENABLED", "Targets": [ { "Id": "SendToSNS", "Arn": { "Ref": "SNSTopic" }, "RoleArn": { "Fn::GetAtt": [ "EventBridgeRole", "Arn" ] } } ] } }, "EventBridgeRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/AmazonSNSFullAccess" ] } } }, "Outputs": { "BucketName": { "Description": "Name of the S3 bucket", "Value": { "Ref": "S3Bucket" } }, "SNSTopicARN": { "Description": "ARN of the SNS topic", "Value": { "Ref": "SNSTopic" } }, "EmailSubscription": { "Description": "Email address for notifications", "Value": { "Ref": "EmailAddress" } } } }