

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

# 群組生命週期事件：監控資源群組是否有變更
<a name="monitor-groups"></a>

使用 AWS Resource Groups 將資源組織成群組後，您可以監控這些群組是否有以*事件*形式公開給您的變更。您可以收到有關群組事件的通知，做為您採取某種動作的訊號。例如，您可以設定每當群組的成員資格變更時傳送的通知。您可以使用新增群組成員的事件來觸發 Lambda 函數，以程式設計方式檢閱變更，以確保新的群組成員符合組織設定的合規要求。這類 Lambda 函數可以為不符合這些要求的任何新群組成員執行自動修復。移除群組成員所造成的事件可能會觸發執行任何必要清除的 Lambda 函數，例如刪除連結的資源。

透過開啟資源群組的群組生命週期事件，就能讓 Amazon EventBridge 擷取有關群組變更的事件，並將其提供給 EventBridge 支援的各種目標服務。然後，您可以將這些目標服務設定為自動執行您的案例所需的任何動作。這些目標包括各種 AWS 服務，例如 Amazon Simple Notification Service (Amazon SNS)、Amazon Simple Queue Service (Amazon SQS) 和 AWS Lambda。使用 Lambda 之類的服務，您的事件可以觸發*使用程式碼來執行您需要的任何動作的程式設計*回應。如需您可以使用 EventBridge 鎖定 AWS 的服務清單，請參閱《[Amazon EventBridge 使用者指南》中的 Amazon EventBridge 目標](https://docs.aws.amazon.com//eventbridge/latest/userguide/eventbridge-targets.html)。 * EventBridge *

當您開啟群組生命週期事件時， 會 AWS Resource Groups 建立下列項目：
+ 具有許可的 AWS Identity and Access Management (IAM) 服務連結角色，可監控資源的標籤和 CloudFormation 堆疊是否有任何變更，以及做為堆疊一部分的資源是否有任何變更。
+ 資源群組受管 EventBridge 規則，可擷取資源的任何標籤或堆疊變更的詳細資訊。EventBridge 使用此規則來通知資源群組這些變更。然後，資源群組會產生成員資格事件以傳送至 EventBridge，以供您的自訂規則處理。

服務連結角色*只能*由 資源群組服務擔任。如需 資源群組用於此功能之服務連結角色的詳細資訊，請參閱 [使用資源群組的服務連結角色](security_iam_service-linked-roles.md)。

開啟此功能時，當您對資源群組進行任何下列變更時，資源群組會產生事件：
+ 建立新的資源群組。
+ 更新定義查詢[型資源群組成員資格的查詢](gettingstarted-query.md)。
+ 更新[服務連結資源群組](https://docs.aws.amazon.com//ARG/latest/APIReference/about-slg.html)的組態。
+ 更新資源群組的描述。
+ 刪除資源群組。
+ 透過從群組新增或移除資源，變更資源群組的成員資格。當標籤變更或 CloudFormation 堆疊變更時，也可能發生成員資格變更。

**重要**  
若要成功接收和回應群組事件，您必須變更資源群組和 EventBridge。您可以按任何順序執行變更，但在對這兩個服務進行變更之前，不會將群組事件發佈至 EventBridge 目標。
資源群組變更不包含附加至資源群組本身之任何標籤的變更。若要根據群組的標籤變更產生事件，您必須使用使用`aws.tag`來源的 EventBridge 規則，而非`aws.resource-groups`來源。如需詳細資訊，請參閱《*Amazon EventBridge 使用者指南*》中的在[AWS 資源上標記變更事件](https://docs.aws.amazon.com//eventbridge/latest/userguide/event-types.html#tag-event-types)。

**Topics**
+ [在資源群組中開啟群組生命週期事件](monitor-groups-turn-on.md)
+ [建立 EventBridge 規則以擷取群組生命週期事件並發佈通知](monitor-groups-create-rule.md)
+ [關閉群組生命週期事件](monitor-groups-turn-off.md)
+ [資源群組生命週期事件的結構和語法](monitor-groups-syntax.md)

# 在資源群組中開啟群組生命週期事件
<a name="monitor-groups-turn-on"></a>

若要接收資源群組生命週期變更的通知，您可以開啟群組生命週期事件。然後，資源群組會提供群組對 Amazon EventBridge 變更的相關資訊。在 EventBridge 中，您可以使用[您在 EventBridge 服務中定義的規則](monitor-groups-create-rule.md)來評估和處理變更。

**最低許可**  
若要在 中開啟群組生命週期事件 AWS 帳戶，您必須以具有下列許可的 AWS Identity and Access Management (IAM) 主體身分登入：  
`resource-groups:UpdateAccountSettings`
`iam:CreateServiceLinkedRole`
`events:PutRule`
`events:PutTargets`
`events:DescribeRule`
`events:ListTargetsByRule`
`cloudformation:DescribeStacks`
`cloudformation:ListStackResources`
`tag:GetResources`

當您一開始在 中開啟群組生命週期事件時 AWS 帳戶，資源群組會建立[名為 的服務連結角色`AWSServiceRoleForResourceGroups`](security_iam_service-linked-roles.md)。此受管角色具有使用資源群組受管 EventBridge 規則的許可。此規則會監控連接至 資源的 CloudFormation 標籤，以及您帳戶中的堆疊是否有任何變更。然後，資源群組會將這些變更發佈至 Amazon EventBridge 中的預設事件匯流排。此服務也會建立名為 的 EventBridge 受管規則`[Managed.ResourceGroups.TagChangeEvents](https://console.aws.amazon.com/events/home#/eventbus/default/rules/Managed.ResourceGroups.TagChangeEvents)`。此規則會擷取 資源的標籤變更詳細資訊。這可讓資源群組產生成員資格事件以傳送至 EventBridge，以供您的自訂規則處理。然後，您的 EventBridge 規則可以透過傳送通知到規則設定的目標來回應事件。

完成這些步驟後，尋找這些事件的規則應該會在幾分鐘內開始接收它們。

您可以使用 或使用來自 AWS CLI AWS 管理主控台 或其中一個 SDK APIs命令來開啟群組生命週期事件。

**注意**  
如果您的資源群組配額太高，則無法開啟群組生命週期事件。如需詳細資訊，請參閱[檢視服務配額](https://docs.aws.amazon.com/servicequotas/latest/userguide/gs-request-quota)。

------
#### [ AWS 管理主控台 ]

**在資源群組主控台中開啟群組生命週期事件**

1. 在資源群組主控台中開啟**[設定](https://console.aws.amazon.com/resource-groups/groups/settings)**頁面。

1. 在**群組生命週期事件**區段中，選擇**通知關閉**旁的開關。

1. 在確認對話方塊中，選擇**開啟通知**。

   功能開關會顯示**通知已開啟**。

這會完成程序的第一部分。開啟事件通知後，您可以在 [Amazon EventBridge 中建立擷取事件的規則](monitor-groups-create-rule.md)，並將其傳送至特定 AWS 服務 進行處理。

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

**使用 AWS CLI 或 AWS SDKs開啟群組生命週期事件**  
下列範例示範如何使用 AWS CLI 來開啟資源群組中的群組生命週期事件。完全依照所示，使用 服務主體參數輸入 命令。輸出會顯示功能的目前狀態和所需狀態。

```
$ aws resource-groups update-account-settings \
    --group-lifecycle-events-desired-status ACTIVE
{
    "AccountSettings": {
        "GroupLifecycleEventsDesiredStatus": "ACTIVE",
        "GroupLifecycleEventsStatus": "IN_PROGRESS"
    }
}
```

您可以執行下列範例命令，確認功能已開啟。當兩個狀態欄位顯示相同的值時，操作即完成。

```
$ aws resource-groups get-account-settings
{
    "AccountSettings": {
        "GroupLifecycleEventsDesiredStatus": "ACTIVE",
        "GroupLifecycleEventsStatus": "ACTIVE"
    }
}
```

如需詳細資訊，請參閱下列資源：
+ AWS CLI – [aws resource-groups update-account-settings ](https://docs.aws.amazon.com//cli/latest/reference/resource-groups/update-account-settings.html)和 [aws resource-groups get-account-settings](https://docs.aws.amazon.com//cli/latest/reference/resource-groups/get-account-settings.html)
+ API – [UpdateAccountSettings](https://docs.aws.amazon.com//ARG/latest/APIReference/API_UpdateAccountSettings.html) 和 [GetAccountSettings](https://docs.aws.amazon.com//ARG/latest/APIReference/API_GetAccountSettings.html)

------

# 建立 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)。

# 關閉群組生命週期事件
<a name="monitor-groups-turn-off"></a>

您可以關閉群組生命週期事件， AWS Resource Groups 以停止向 Amazon EventBridge 發出事件。您可以使用 或使用來自 AWS 管理主控台 或 AWS CLI 其中一個 SDK APIs命令來執行此操作。

**注意**  
關閉群組生命週期事件會刪除用於掃描資源標籤和 CloudFormation 堆疊是否有變更的資源群組受管 EventBridge 規則。資源群組無法再將這些變更傳遞至 EventBridge。您在 EventBridge 中定義的任何尋找資源群組事件的規則都會停止接收要處理的事件。如果您未來想要再次開啟群組生命週期事件，您可以停用規則。如果您不打算再次使用這些規則，您可以刪除它們。如需詳細資訊，請參閱《Amazon [ EventBridge 使用者指南》中的停用或刪除](https://docs.aws.amazon.com//eventbridge/latest/userguide/delete-or-disable-rule.html) EventBridge 規則。 * EventBridge *  
關閉群組生命週期事件***並不會***刪除服務連結角色。如果您想要使用 IAM[，可以手動刪除服務連結角色](security_iam_service-linked-roles.md#delete-service-linked-role)。如果您稍後需要再次開啟群組生命週期事件，且服務連結角色不存在，資源群組會自動重新建立。

**最低許可**  
若要關閉目前 中的群組生命週期事件 AWS 帳戶，您必須以具有下列許可的 AWS Identity and Access Management (IAM) 主體身分登入：  
`resource-groups:UpdateAccountSettings`
`events:DeleteRule`
`events:RemoveTargets`
`events:DescribeRule`
`events:ListTargetsByRule`

------
#### [ AWS 管理主控台 ]

**關閉 EventBridge 的群組生命週期事件通知**

1. 在資源群組主控台中開啟**[設定](https://console.aws.amazon.com/resource-groups/groups/settings)**頁面。

1. 在**群組生命週期事件**區段中，選擇**開啟通知**旁的切換。

1. 在確認對話方塊中，選擇**關閉通知**。

   顯示功能開關：**關閉事件通知**。

此時，資源群組不會再將事件傳送至 EventBridge 預設事件匯流排，以及您不再收到要處理之群組通知事件的任何規則。您可以選擇性地刪除這些規則以完成清除。

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

**關閉 EventBridge 的群組生命週期事件通知**  
下列範例示範如何使用 AWS CLI 關閉 資源群組中的群組生命週期事件。

```
$ aws resource-groups update-account-settings \
    ----group-lifecycle-events-desired-status INACTIVE
{
    "AccountSettings": {
        "GroupLifecycleEventsDesiredStatus": "INACTIVE",
        "GroupLifecycleEventsStatus": "INACTIVE"
    }
}
```

------

如需詳細資訊，請參閱下列資源：
+ AWS CLI – [aws resource-groups update-account-settings ](https://docs.aws.amazon.com//cli/latest/reference/resource-groups/update-account-settings.html)和 [aws resource-groups get-account-settings](https://docs.aws.amazon.com//cli/latest/reference/resource-groups/get-account-settings.html)
+ API – [UpdateAccountSettings](https://docs.aws.amazon.com//ARG/latest/APIReference/API_UpdateAccountSettings.html) 和 [GetAccountSettings](https://docs.aws.amazon.com//ARG/latest/APIReference/API_GetAccountSettings.html)

# 資源群組生命週期事件的結構和語法
<a name="monitor-groups-syntax"></a>

**Topics**
+ [`detail` 欄位的結構](monitor-groups-syntax-detail.md)
+ [不同使用案例的 EventBridge 自訂事件模式範例](monitor-groups-example-eventbridge-filters.md)

的生命週期事件採用下列一般格式的 [JSON](https://json.org) 物件字串 AWS Resource Groups 形式。

```
{
    "version": "0",
    "id": "08f00e24-2e30-ec44-b824-8acddf1ac868",
    "detail-type": "ResourceGroups Group ... Change",
    "source": "aws.resource-groups",
    "account": "123456789012",
    "time": "2020-09-29T09:59:01Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:resource-groups:us-east-1:123456789012:group/MyGroupName"
    ],
    "detail": {
        ...
    }
}
```

如需所有 Amazon EventBridge 事件通用欄位的詳細資訊，請參閱《[Amazon EventBridge 使用者指南](https://docs.aws.amazon.com//eventbridge/latest/userguide/aws-events.html)*》中的 Amazon EventBridge *事件。下表說明資源群組特有的詳細資訊。


| 欄位名稱 | Type | Description | 
| --- | --- | --- | 
| detail-type | String |  對於資源群組， `detail-type` 欄位一律是下列其中一個值： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/ARG/latest/userguide/monitor-groups-syntax.html)  | 
| source | String | 對於資源群組，此值一律為 "aws.resource-groups"。 | 
| resources | Amazon Resource Name (ARNs陣列 |  此欄位一律包含群組的 [Amazon 資源名稱 (ARN)](https://docs.aws.amazon.com//general/latest/gr/aws-arns-and-namespaces.html)，其中包含觸發此事件的變更。 如果適用，此欄位也可以包含從群組新增或移除的任何資源ARNs。  | 
|  `detail`  | JSON 物件字串 | 這是事件的承載。detail 欄位的內容會根據 的值而有所不同detail-type。[如需詳細資訊，請參閱下一節。](monitor-groups-syntax-detail.md) | 

# `detail` 欄位的結構
<a name="monitor-groups-syntax-detail"></a>

`detail` 欄位包含特定變更的所有資源群組服務特定詳細資訊。`detail` 欄位可以採用兩種形式之一：群組狀態變更或成員資格變更，以上一節所述的`detail-type`欄位值為基礎。

**重要**  
這些事件中的資源群組是透過群組 ARN 和包含 [UUID](https://wikipedia.org/wiki/Universally_unique_identifier) `"unique-id"` 的欄位的組合來識別。透過將 UUID 包含在資源群組的身分中，您可以區分已刪除的群組和稍後以相同名稱建立的不同群組。我們建議您將 ARN 和唯一 ID 的串連視為與這些事件互動之程式中 群組的金鑰。

## 群組狀態變更
<a name="monitor-groups-syntax-detail-state-change"></a>

`"detail-type": "ResourceGroups Group State Change"`

此`detail-type`值表示群組本身的狀態已變更，包括其中繼資料。此變更會在建立、更新或刪除群組時發生，如 中的 `"change"` 欄位所示`detail`。

指定`detail-type`此項目時， `details`區段中包含的資訊包含下表所述的欄位。


| 欄位名稱 | Type | 說明 | 
| --- | --- | --- | 
| event-sequence | Double | 單調增加數字，指定特定群組的事件序列。當您刪除群組並使用相同名稱建立另一個群組時，數字會重設。 | 
| group | [`Group`](#monitor-groups-syntax-detail-group-object) JSON 物件 | 與事件相關聯的群組物件，依其 ARN、名稱和唯一 ID。 | 
| state-change | String | 發生的狀態變更類型。可以是下列任何值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/ARG/latest/userguide/monitor-groups-syntax-detail.html) | 
| old-state  | GroupState JSON 物件 | 變更前的群組狀態。物件僅包含已變更之屬性的值。 | 
|  `new-state`  | GroupState JSON 物件 | 變更後的 群組狀態。物件僅包含已變更之屬性的值。 | 

`group` JSON 物件包含下表所述的元素。


| 欄位名稱 | Type | Description | 
| --- | --- | --- | 
| arn | String | 群組的 ARN。 | 
| name | String | 群組的易記名稱。 | 
| unique-id | GUID | 唯一的 GUID 值，可區分已刪除的群組和稍後以相同名稱和 ARN 建立的不同群組。在程式碼中使用這些事件時，使用 ARN 和此值的串連做為群組的唯一索引鍵。 | 

`GroupState` JSON 物件包含下表所述的元素。


| 欄位名稱 | Type | Description | 
| --- | --- | --- | 
| description | String | 客戶提供的資源群組描述。 | 
| resource-query | ResourceQuery JSON 物件 | 定義群組成員之查詢的 JSON 表示法。此欄位僅適用於以查詢為基礎的群組。此欄位的語法是由 [ResourceQuery API 資料類型](https://docs.aws.amazon.com//organizations/latest/APIReference/API_ResourceQuery.html)所定義。此範例包含在[建立](#monitor-groups-syntax-detail-state-change-create)和[更新](#monitor-groups-syntax-detail-state-change-update)事件範例中。 | 
| group-configuration | Configuration JSON 物件 | 與服務連結群組相關聯的組態參數的 JSON 表示法。如需詳細資訊，請參閱 AWS Resource Groups API 參考中的[資源群組的服務組態](https://docs.aws.amazon.com/ARG/latest/APIReference/about-slg.html)。 | 

下列每個程式碼範例都會說明每種`state-change`類型的 `detail` 欄位內容。

### 建立
<a name="monitor-groups-syntax-detail-state-change-create"></a>

`"state-change": "create"` 

事件指出已建立新的群組。事件會攜帶在建立群組期間設定的所有群組中繼資料屬性。除非群組是空的，否則此事件通常會跟隨多個群組成員資格事件的其中之一。具有 null 值的屬性不會顯示在事件內文中。

下列範例事件指出新建立的資源群組，名為 `my-service-group`。在此範例中， 群組使用標籤型查詢，只比對具有標籤 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體`"project"="my-service"`。

```
{
    "version": "0",
    "id": "08f00e24-2e30-ec44-b824-8acddf1ac868",
    "detail-type": "ResourceGroups Group State Change",
    "source": "aws.resource-groups",
    "account": "123456789012",
    "time": "2020-09-29T09:59:01Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:resource-groups:us-east-1:123456789012:group/my-service-group"
    ],
    "detail": {
        "event-sequence": 1.0,
        "state-change": "create", 
        "group": {
            "arn": "arn:aws:resource-groups:us-east-1:123456789012:group/my-service-group",
            "name": "my-service-group",
            "unique-id": "3dd07ab7-3228-4410-8cdc-6c4a10fcceea"
        },        
        "new-state": {
            "resource-query": {
                "type": "TAG_FILTERS_1_0",
                "query": "{
                    \"ResourceTypeFilters\": [\"AWS::EC2::Instance\"],
                    \"TagFilters\": [{\"Key\":\"project\", \"Values\":[\"my-service\"}]
                }"
            }
        }
    }
}
```

### 更新
<a name="monitor-groups-syntax-detail-state-change-update"></a>

`"state-change": "update"`

事件指出現有群組已透過某種方式修改。事件只會承載從先前狀態變更的屬性。未變更的屬性不會顯示在事件內文中。

下列範例事件指出上一個範例資源群組中的標籤型查詢已修改為在群組中也包含 Amazon EC2 磁碟區資源。

```
{
    "version": "0",
    "id": "08f00e24-2e30-ec44-b824-8acddf1ac868",
    "detail-type": "ResourceGroups Group State Change",
    "source": "aws.resource-groups",
    "account": "123456789012",
    "time": "2020-09-29T09:59:01Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:resource-groups:us-east-1:123456789012:group/my-service-group"
    ],
    "detail": {
        "event-sequence": 3.0,
        "state-change": "update",
        "group": {
            "arn": "arn:aws:resource-groups:us-east-1:123456789012:group/my-service-group",
            "name": "my-service",
            "unique-id": "3dd07ab7-3228-4410-8cdc-6c4a10fcceea"
        },        
        "new-state": {
            "resource-query": {
                "type": "TAG_FILTERS_1_0",
                "query": "{
                    \"ResourceTypeFilters\": [\"AWS::EC2::Instance\", \"AWS::EC2::Volume\"],
                    \"TagFilters\": [{\"Key\":\"project\", \"Values\":[\"my-service\"}]
                }"
            }
        },
        "old-state": {
            "resource-query": {
                "type": "TAG_FILTERS_1_0",
                "query": "{
                    \"ResourceTypeFilters\": [\"AWS::EC2::Instance\"],
                    \"TagFilters\": [{\"Key\":\"Project\", \"Values\":[\"my-service\"}]
                }"
            }
        }
    }
}
```

### 刪除
<a name="monitor-groups-syntax-detail-state-change-delete"></a>

`"state-change": "delete"`

事件指出現有群組已刪除。詳細資訊欄位不包含群組識別以外的中繼資料。根據定義， `event-sequence` 欄位會在此事件之後重設為此 `arn`和 的最後一個事件`unique-id`。

```
{
    "version": "0",
    "id": "08f00e24-2e30-ec44-b824-8acddf1ac868",
    "detail-type": "ResourceGroups Group State Change",
    "source": "aws.resource-groups",
    "account": "123456789012",
    "time": "2020-09-29T09:59:01Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:resource-groups:us-east-1:123456789012:group/my-service"
    ],
    "detail": {
        "event-sequence": 4.0,   
        "state-change": "delete",
        "group": {
            "arn": "arn:aws:resource-groups:us-east-1:123456789012:group/my-service",
            "name": "my-service",
            "unique-id": "3dd07ab7-3228-4410-8cdc-6c4a10fcceea"
        }
    }
}
```

## 群組成員資格變更
<a name="monitor-groups-syntax-detail-membership-change"></a>

`"detail-type": "ResourceGroups Group Membership Change"`

此`detail-type`值表示群組的成員資格已由新增至群組或從群組中移除的資源所變更。指定`detail-type`此選項時，最上層`resources`欄位會包含其成員資格已變更之群組的 ARN，以及新增至群組或從群組中移除之任何資源的 ARNs。

指定`detail-type`此項目時， `details`區段中包含的資訊包含下表所述的欄位。


| 欄位名稱 | Type | 說明 | 
| --- | --- | --- | 
| event-sequence | Double | 單調增加數字，指出特定群組的事件序列。刪除群組且其唯一 ID 變更時，數字會重設。 | 
| group | Group JSON 物件 | 依事件的 ARN、名稱和唯一 ID 來識別與事件相關聯的群組物件。 | 
|  `resources`  | ResourceChange JSON 物件陣列 |  群組成員資格已變更的資源陣列。 此`ResourceChange`物件包含每個資源的下列欄位： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/ARG/latest/userguide/monitor-groups-syntax-detail.html)  | 

下列程式碼範例說明典型成員資格變更類型的事件內容。此範例顯示將一個資源新增至群組，以及將一個資源從群組中移除。

```
{
    "version": "0",
    "id": "08f00e24-2e30-ec44-b824-8acddf1ac868",
    "detail-type": "ResourceGroups Group Membership Change",
    "source": "aws.resource-groups",
    "account": "123456789012",
    "time": "2020-09-29T09:59:01Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:resource-groups:us-east-1:123456789012:group/my-service",
        "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111",
        "arn:aws:ec2:us-east-1:123456789012:instance/i-efef2222"
    ],
    "detail": {
        "event-sequence": 2.0,
        "group": {
            "arn": "arn:aws:resource-groups:us-east-1:123456789012:group/my-service",
            "name": "my-service",
            "unique-id": "3dd07ab7-3228-4410-8cdc-6c4a10fcceea"
        },
        "resources": [
            {
                "membership-change": "add",
                "arn": "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111",
                "resource-type": "AWS::EC2::Instance"
            },
            {
                "membership-change": "remove",
                "arn": "arn:aws:ec2:us-east-1:123456789012:instance/i-efef2222",
                "resource-type": "AWS::EC2::Instance"
            }
        ]
    }
}
```

# 不同使用案例的 EventBridge 自訂事件模式範例
<a name="monitor-groups-example-eventbridge-filters"></a>

下列範例 EventBridge 自訂事件模式會將資源群組產生的事件篩選為僅您對特定事件規則和目標感興趣的事件。

在下列程式碼範例中，如果需要特定群組或資源，請以您自己的資訊取代每個*使用者輸入預留位置*。

所有資源群組事件  

```
{
    "source": [ "aws.resource-groups" ]
}
```

群組狀態或成員資格變更事件  
下列程式碼範例適用於所有群組*狀態*變更。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group State Change " ]
}
```
下列程式碼範例適用於所有群組*成員*資格變更。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group Membership Change" ]
}
```

特定群組的事件  

```
{
    "source": [ "aws.resource-groups" ],
    "detail": {
        "group": {
            "arn": [ "my-group-arn" ]
        }
    }
}
```
上一個範例會擷取指定群組的變更。當群組是另一個群組的成員資源時，下列範例會執行相同的動作，也會擷取變更。  

```
{
    "source": [ "aws.resource-groups" ],
    "resources": [ "my-group-arn" ]
}
```

特定資源的事件  
您只能針對特定成員資源篩選群組成員變更事件。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group Membership Change " ],
    "resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f" ]
}
```

特定資源類型的事件  
您可以使用字首比對搭配 ARNs 來比對特定資源類型的事件。  

```
{
    "source": [ "aws.resource-groups" ],
    "resources": [
        { "prefix": "arn:aws:ec2:us-east-1:123456789012:instance" } 
    ]
}
```
或者，您可以使用識別`resource-type`符來使用完全相符，可能簡潔地比對多種類型。與先前的範例不同，下列範例只會比對群組成員資格變更事件，因為群組狀態變更事件不會在其欄位中包含 `resources` 欄位`detail`。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail": {
        "resources": {
                "resource-type": [ "AWS::EC2::Instance", "AWS::EC2::Volume" ]
         }
    }
}
```

所有資源移除事件  

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group Membership Change" ],
    "detail": {
        "resources": {
                "membership-change": [ "remove" ]
        }
    }
}
```

特定資源的所有資源移除事件  

```
 {
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group Membership Change" ],
    "detail": {
        "resources": {
                "membership-change": [ "remove" ],
                "arn": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f" ]
         }
    }
}
```
您無法針對此類型的事件篩選，使用本節第一個範例中使用的**頂層**`resources`陣列。這是因為頂層`resources`元素中的資源可能是要新增至群組的資源，而且事件仍然相符。換句話說，下列程式碼範例可能會傳回非預期的事件。反之，請使用上一個範例中顯示的語法。  

```
{
    "source": [ "aws.resource-groups" ],
    "detail-type": [ "ResourceGroups Group Membership Change" ],
    "resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f" ],
    "detail": {
        "resources": {
                "membership-change": [ "remove" ]
         }
     }
}
```