

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

# 資源群組生命週期事件的結構和語法
<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" ]
         }
     }
}
```