

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

# 建立 EventBridge 規則以擷取群組生命週期事件並發佈通知
<a name="monitor-groups-create-rule"></a>

您可以在 中[開啟資源群組的群組生命週期事件](monitor-groups-turn-on.md) AWS Resource Groups ，將事件發佈至 Amazon EventBridge。然後，您可以建立 EventBridge 規則，透過將它們傳送到其他 AWS 服務 進行進一步處理來回應這些事件。

------
#### [ AWS CLI ]

在 EventBridge 中建立規則的程序，該規則會擷取事件並將其傳送至您想要的目標服務，並採用兩個不同的 CLI 命令：

1. [建立 EventBridge 規則以擷取您想要的事件](#monitor-cli-create-rule)

1. [將可處理事件的目標連接到 EventBridge 規則 ](#monitor-cli-attach-target)

步驟 1：建立 EventBridge 規則以擷取事件  
下列 AWS CLI `[put-rule](https://docs.aws.amazon.com//cli/latest/reference/events/put-rule.html)`範例命令會建立 EventBridge 規則，擷取***所有***資源群組生命週期事件變更。  

```
$ aws events put-rule \
    --name "CatchAllResourceGroupEvents" \
    --event-pattern '{"source":["aws.resource-groups"]}'
{
    "RuleArn": "arn:aws:events:us-east-1:123456789012:rule/CatchAllResourceGroupEvents"
}
```
輸出包含新規則的 Amazon Resource Name (ARN)。  
包含引號字串的參數值，會根據您使用的作業系統和 Shell 具有不同的格式規則。對於本指南中的範例，我們顯示適用於 Linux BASH shell 的命令。如需針對其他作業系統使用內嵌引號格式化字串的說明，例如 Windows 命令提示字元，請參閱*AWS Command Line Interface *[《 使用者指南》中的在字串內使用引號](https://docs.aws.amazon.com//cli/latest/userguide/cli-usage-parameters-quoting-strings.html#cli-usage-parameters-quoting-strings-containing)。  
隨著參數字串變得更複雜，[從文字檔案接受參數值](https://docs.aws.amazon.com//cli/latest/userguide/cli-usage-parameters-file.html)可能更容易，也較少發生錯誤，而不是直接在命令列中輸入參數值。
下列事件模式會將事件限制為僅與指定群組相關的事件，並由其 ARN 識別。此事件模式是複雜的 JSON 字串，壓縮為單行且正確逸出的 JSON 字串時，其可讀性會大幅降低。您可以改為將它存放在檔案中。  
將事件模式 JSON 字串存放在檔案中。在下列程式碼範例中，檔案為 `eventpattern.txt`。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail": {
        "group": {
            "arn": [ "my-resource-group-arn" ]
        }
    }
}
```
然後，發出下列命令來建立規則，從 檔案擷取自訂事件模式。  

```
$ aws events put-rule \
    --name "CatchResourceGroupEventsForMyGroup" \
    --event-pattern file://eventpattern.txt
{
    "RuleArn": "arn:aws:events:us-east-1:123456789012:rule/CatchResourceGroupEventsForMyGroup"
}
```
若要擷取其他類型的資源群組事件，請將`--event-pattern`字串取代為篩選條件，如區段 所示[不同使用案例的 EventBridge 自訂事件模式範例](monitor-groups-example-eventbridge-filters.md)。

步驟 2：將可處理事件的目標連接到 EventBridge 規則   
現在您有一個規則可以擷取感興趣的事件，您可以連接一或多個目標，對事件執行某種類型的處理。  
下列 AWS CLI `[put-targets](https://docs.aws.amazon.com//cli/latest/reference/events/put-targets.html)`命令會將名為 的 Amazon Simple Notification Service (Amazon SNS) 主題連接至您在先前範例中建立`my-sns-topic`的規則。當規則中指定的群組發生變更時，主題的所有訂閱者都會收到通知。  

```
$ aws events put-targets \
    --rule CatchResourceGroupEventsForMyGroup \
    --targets Id=1,Arn=arn:aws:sns:us-east-1:123456789012:my-sns-topic
{
    "FailedEntryCount": 0,
    "FailedEntries": []
}
```
此時，符合規則中事件模式的任何群組變更都會自動傳送至設定的目標。如果如先前範例所示，目標為 Amazon SNS 主題，則主題的所有訂閱者都會收到包含事件的訊息，如中所述[資源群組生命週期事件的結構和語法](monitor-groups-syntax.md)。

如需詳細資訊，請參閱下列資源：
+ AWS CLI – [aws 事件 put-rule](https://docs.aws.amazon.com//cli/latest/reference/events/put-rule.html) 和 [aws 事件 put-targets](https://docs.aws.amazon.com//cli/latest/reference/events/put-targets.html)
+ API – [PutRule](https://docs.aws.amazon.com//eventbridge/latest/APIReference/API_PutRule.html) 和 [PutTargets](https://docs.aws.amazon.com//eventbridge/latest/APIReference/API_PutTargets.html)

------

## 建立規則以僅擷取特定群組生命週期事件類型
<a name="monitor-groups-create-rule-custom"></a>

您可以建立具有自訂事件模式的規則，該模式只會擷取您感興趣的事件。如需如何使用自訂事件模式篩選傳入事件的完整詳細資訊，請參閱《[Amazon EventBridge 使用者指南](https://docs.aws.amazon.com//eventbridge/latest/userguide/eventbridge-and-event-patterns.html)*》中的 Amazon EventBridge *事件。

例如，假設您希望規則僅處理那些表示建立新資源群組的資源群組通知。您可以使用類似下列範例的自訂事件模式。

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group State Change" ],
    "detail": {
        "state-change": "create"
    }
}
```

該篩選條件只會擷取指定欄位中具有這些確切值的事件。如需可供您比對之欄位的完整清單，請參閱 [資源群組生命週期事件的結構和語法](monitor-groups-syntax.md)。