

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

# AWS Identity and Access Management Access Analyzer 使用 Amazon EventBridge 進行監控
<a name="access-analyzer-eventbridge"></a>

使用本主題中的資訊來了解如何使用 Amazon EventBridge 監控 IAM Access Analyzer 問題清單及存取預覽。EventBridge 是 Amazon CloudWatch Events 的新版本。

## 問題清單事件
<a name="access-analyzer-events-findings"></a>

IAM Access Analyzer 會為每個產生的問題清單、對現有問題清單的狀態所做的變更，以及在刪除問題清單時，將事件傳送至 EventBridge。若要接收問題清單的相關問題清單和通知，您必須在 Amazon EventBridge 中建立事件規則。建立事件規則時，您也可以根據規則指定要觸發的目標動作。例如，您可以建立事件規則，以便在從 IAM Access Analyzer 收到新問題清單的事件時觸發 Amazon SNS 主題。有關資源控制政策 (RCP) 的詳細資訊，請參閱事件詳細資訊一節。

## 存取預覽事件
<a name="access-analyzer-access-preview-events"></a>

IAM Access Analyzer 會將事件傳送到每個存取預覽的 EventBridge 並變更為其狀態。這包括第一次建立存取預覽 (狀態為「建立」)、存取預覽完成 (狀態為「已完成」) 或存取預覽建立失敗 (狀態為「失敗」) 時的事件。若要接收存取預覽的相關通知，您必須在 EventBridge 中建立事件規則。建立事件規則時，您可以根據規則指定要觸發的目標動作。例如，您可以建立事件規則，以便在從 IAM Access Analyzer 收到的已完成存取預覽時觸發 Amazon SNS 主題。

## 事件通知頻率
<a name="access-analyzer-event-frequency"></a>

IAM Access Analyzer 會在您帳戶中發生事件後約一小時內，將新問題清單和包含狀態更新問題清單的事件傳送給 EventBridge。IAM Access Analyzer 也會在刪除已解析的問題清單時將事件傳送給 EventBridge，因為保留期間已過期。對於因產生問題清單的分析器遭到刪除而刪除的問題清單，系統會在分析器遭到刪除後約 24 小時將事件傳送到 EventBridge。問題清單遭刪除時，問題清單的狀態不會變更。相反地，`isDeleted` 屬性會設定為 `true`。IAM Access Analyzer 也會為新建立的存取預覽傳送事件，以及對 EventBridge 的存取預覽狀態變更。



## 外部存取權調查結果事件範例
<a name="access-analyzer-event-example"></a>

以下為傳送至 EventBridge 的 IAM Access Analyzer 外部存取權調查結果事件範例。所列的 `id` 是 EventBridge 中事件的 ID。若要進一步了解，請參閱 [EventBridge 中的事件和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-and-event-patterns.html)。

在 `detail` 物件中，`accountId` 和 `region` 屬性的值參考結果所報告的帳戶和區域。`isDeleted` 屬性會指出事件是否來自要刪除的問題清單。`id` 是結果 ID。`resources` 陣列是具有產生結果的分析器 ARN 的單例。

```
{
    "account": "111122223333",
    "detail": {
        "accountId": "111122223333",
        "action": [
            "s3:GetObject"
        ],
        "analyzedAt": "2019-11-21T01:22:22Z",
        "condition": {},
        "createdAt": "2019-11-20T04:58:50Z",
        "id": "22222222-dcba-4444-dcba-333333333333",
        "isDeleted": false,
        "isPublic": false,
        "principal": {
            "AWS": "999988887777"
        },
        "region": "us-west-2",
        "resource": "arn:aws:s3:::amzn-s3-demo-bucket",
        "resourceType": "AWS::S3::Bucket",
        "status": "ACTIVE",
        "updatedAt": "2019-11-21T01:14:07Z",
        "version": "1.0"
    },
    "detail-type": "Access Analyzer Finding",
    "id": "11111111-2222-4444-aaaa-333333333333",
    "region": "us-west-2",
    "resources": [
        "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/MyAnalyzer"
    ],
    "source": "aws.access-analyzer",
    "time": "2019-11-21T01:22:33Z",
    "version": "0"
}
```

IAM Access Analyzer 也會將事件傳送至錯誤問題清單的 EventBridge。錯誤問題清單是 IAM Access Analyzer 無法分析資源時產生的問題清單。錯誤問題清單的事件包括下列範例所示的 `error` 屬性。

```
{
    "account": "111122223333",
    "detail": {
        "accountId": "111122223333",
        "analyzedAt": "2019-11-21T01:22:22Z",
        "createdAt": "2019-11-20T04:58:50Z",
        "error": "ACCESS_DENIED",
        "id": "22222222-dcba-4444-dcba-333333333333",
        "isDeleted": false,
        "region": "us-west-2",
        "resource": "arn:aws:s3:::amzn-s3-demo-bucket",
        "resourceType": "AWS::S3::Bucket",
        "status": "ACTIVE",
        "updatedAt": "2019-11-21T01:14:07Z",
        "version": "1.0"
    },
    "detail-type": "Access Analyzer Finding",
    "id": "11111111-2222-4444-aaaa-333333333333",
    "region": "us-west-2",
    "resources": [
        "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/MyAnalyzer"
    ],
    "source": "aws.access-analyzer",
    "time": "2019-11-21T01:22:33Z",
    "version": "0"
}
```

## 內部存取調查結果事件範例
<a name="access-analyzer-event-example-internal-access-findings-events"></a>

以下為傳送至 EventBridge 的 IAM Access Analyzer 內部存取調查結果事件範例。所列的 `id` 是 EventBridge 中事件的 ID。若要進一步了解，請參閱 [EventBridge 中的事件和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-and-event-patterns.html)。

在 `detail` 物件中，`accountId` 和 `principalOwnerAccount` 屬性的值指調查結果中報告的主體的帳戶。`isDeleted` 屬性會指出事件是否來自要刪除的問題清單。`id` 是結果 ID。`resource` 是產生調查結果的分析器的 ARN。

```
{
    "version": "0",
    "id": "b45c3678-c278-b593-6121-c155259ce1b5",
    "detail-type": "Internal Access Finding",
    "source": "aws.access-analyzer",
    "account": "111122223333",
    "time": "2025-04-08T19:42:49Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:access-analyzer:us-east-1:111122223333:analyzer/testAnalyzer"
    ],
    "detail": {
        "accessType": "INTRA_ACCOUNT",
        "action": [
            "s3:GetObject"
        ],
        "analyzedAt": "2025-04-08T03:18:43.509465073Z", 
        "condition": {},
        "createdAt": "2025-04-07T21:33:49.914099224Z",
        "id": "11111111-2222-4444-aaaa-333333333333",
        "isDeleted": false,
        "findingType": "InternalAccess",
        "principal": {
            "AWS": "arn:aws:iam::111122223333:role/MyRole_6"
        },
        "principalOwnerAccount": "111122223333",
        "principalType": "IAM_ROLE",
        "resource": "arn:aws:s3:::critical-data",
        "resourceControlPolicyRestrictionType": "NOT_APPLICABLE",
        "accountId": "111122223333",
        "resourceType": "AWS::S3::Bucket",
        "serviceControlPolicyRestrictionType": "NOT_APPLICABLE",
        "status": "ACTIVE",
        "updatedAt": "2025-04-08T03:22:12.654688231Z",
        "version": "1.0"
    }
}
```

IAM Access Analyzer 也會將事件傳送至錯誤問題清單的 EventBridge。錯誤問題清單是 IAM Access Analyzer 無法分析資源時產生的問題清單。錯誤問題清單的事件包括下列範例所示的 `error` 屬性。

```
{
    "version": "0",
    "id": "5a94b99b-e87d-a6a7-58c7-f47871532860",
    "detail-type": "Internal Access Finding",
    "source": "aws.access-analyzer-test",
    "account": "444455556666",
    "time": "2025-05-07T11:57:54Z",
    "region": "us-west-2",
    "resources": ["arn:aws:access-analyzer-beta:us-west-2:444455556666:analyzer/example-analyzer"],
    "detail": {
        "analyzedAt": "2025-03-24T19:58:52.512329448Z",
        "createdAt": "2025-03-22T03:30:46.920200692Z",
        "id": "ef573afd-12a5-4095-87a6-bf2f25109895",
        "isDeleted": false,
        "findingType": "InternalAccess",
        "resource": "arn:aws:s3:::test-entity-88",
        "accountId": "111122223333",
        "resourceControlPolicyRestrictionType": "NOT_APPLICABLE",
        "resourceType": "AWS::S3::Bucket",
        "serviceControlPolicyRestrictionType": "NOT_APPLICABLE",
        "error": "ACCESS_DENIED", // can be INTERNAL_ERROR and ACCESS_DENIED
        "status": "ACTIVE",
        "updatedAt": "2025-03-24T20:09:39.176075014Z",
        "version": "1.0"
    }
}
```

## 未使用的存取權調查結果相關事件範例
<a name="access-analyzer-example-unused-access-findings-related-events"></a>

以下為傳送至 EventBridge 的 IAM Access Analyzer 未使用的存取權調查結果事件範例。所列的 `id` 是 EventBridge 中事件的 ID。若要進一步了解，請參閱 [EventBridge 中的事件和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-and-event-patterns.html)。

在 `detail` 物件中，`accountId` 和 `region` 屬性的值參考結果所報告的帳戶和區域。`isDeleted` 屬性會指出事件是否來自要刪除的問題清單。`id` 是結果 ID。

```
{
    "version": "0",
    "id": "dc7ce3ee-114b-3243-e249-7f10f9054b21",
    "detail-type": "Unused Access Finding for IAM entities",
    "source": "aws.access-analyzer",
    "account": "123456789012",
    "time": "2023-09-29T17:31:40Z",
    "region": "us-west-2",
    "resources": [
       "arn:aws:access-analyzer:us-west-2:123456789012:analyzer/integTestLongLivingAnalyzer-DO-NOT-DELETE"
       ],
    "detail": {
        "findingId": "b8ae0460-5d29-4922-b92a-ba956c986277",
        "resource": "arn:aws:iam::111122223333:role/FindingIntegTestFakeRole",
        "resourceType": "AWS::IAM::Role",
        "accountId": "111122223333",
        "createdAt": "2023-09-29T17:29:18.758Z",
        "updatedAt": "2023-09-29T17:29:18.758Z",
        "analyzedAt": "2023-09-29T17:29:18.758Z",
        "previousStatus": "",
        "status": "ACTIVE",
        "version": "62160bda-8e94-46d6-ac97-9670930d8ffb",
        "isDeleted": false,
        "findingType": "UnusedPermission",
        "numberOfUnusedServices": 0,
        "numberOfUnusedActions": 1
        }
    }
```

IAM Access Analyzer 也會將事件傳送至錯誤問題清單的 EventBridge。錯誤問題清單是 IAM Access Analyzer 無法分析資源時產生的問題清單。錯誤問題清單的事件包括下列範例所示的 `error` 屬性。

```
{
    "version": "0",
    "id": "c2e7aa1a-4df7-7652-f33e-64113b8997d4",
    "detail-type": "Unused Access Finding for IAM entities",
    "source": "aws.access-analyzer",
    "account": "111122223333",
    "time": "2023-10-31T20:26:12Z",
    "region": "us-west-2",
    "resources": [
      "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ba811f91-de99-41a4-97c0-7481898b53f2"
      ],
    "detail": {
        "findingId": "b01a34f2-e118-46c9-aef8-0d8526b495c7",
        "resource": "arn:aws:iam::123456789012:role/TestRole",
        "resourceType": "AWS::IAM::Role",
        "accountId": "444455556666",
        "createdAt": "2023-10-31T20:26:08.647Z",
        "updatedAt": "2023-10-31T20:26:09.245Z",
        "analyzedAt": "2023-10-31T20:26:08.525Z",
        "previousStatus": "",
        "status": "ACTIVE",
        "version": "7c7a72a2-7963-4c59-ac71-f0be597010f7",
        "isDeleted": false,
        "findingType": "UnusedIAMRole",
        "error": "INTERNAL_ERROR"
        }
  }
```

## 存取預覽事件範例
<a name="access-analyzer-example-access-preview-events"></a>

下列範例會顯示您建立存取預覽時傳送至 EventBridge 第一個事件的資料。`resources` 陣列是存取預覽相關聯的分析器 ARN 的單例。在 `detail` 物件中，`id` 指的是存取預覽 ID，而 `configuredResources` 是指為其建立存取預覽的資源。`status` 為 `Creating`，並且是指存取預覽狀態。`previousStatus` 未指定，因為剛剛建立存取預覽。

```
{
    "account": "111122223333",
    "detail": {
        "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb",
        "configuredResources": [
            "arn:aws:s3:::amzn-s3-demo-bucket"
        ],
        "createdAt": "2020-02-20T00:00:00.00Z",
        "region": "us-west-2",
        "status": "CREATING",
        "version": "1.0"
    },
    "detail-type": "Access Preview State Change",
    "id": "aaaabbbb-2222-3333-4444-555566667777",
    "region": "us-west-2",
    "resources": [
        "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/MyAnalyzer"
    ],
    "source": "aws.access-analyzer",
    "time": "2020-02-20T00:00:00.00Z",
    "version": "0"
}
```

下列範例顯示針對狀態從 `Creating` 變更為 `Completed` 的存取預覽傳送至 EventBridge 的事件資料。在詳細資料物件中，`id` 是指存取預覽 ID。`status` 和 `previousStatus` 是指存取預覽狀態，其中先前的狀態為 `Creating`，而目前的狀態為 `Completed`。

```
{
    "account": "111122223333",
    "detail": {
        "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb",
        "configuredResources": [
            "arn:aws:s3:::amzn-s3-demo-bucket"
        ],
        "createdAt": "2020-02-20T00:00:00.000Z",
        "previousStatus": "CREATING",
        "region": "us-west-2",
        "status": "COMPLETED",
        "version": "1.0"
    },
    "detail-type": "Access Preview State Change",
    "id": "11112222-3333-4444-5555-666677778888",
    "region": "us-west-2",
    "resources": [
        "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/MyAnalyzer"
    ],
    "source": "aws.access-analyzer",
    "time": "2020-02-20T00:00:00.00Z",
    "version": "0"
}
```

下列範例顯示針對狀態從 `Creating` 變更為 `Failed` 的存取預覽傳送至 EventBridge 的事件資料。在 `detail` 物件中，`id` 是指存取預覽 ID。`status` 和 `previousStatus` 是指存取預覽狀態，其中先前的狀態為 `Creating`，而目前的狀態為 `Failed`。`statusReason` 欄位會提供原因代碼，指出存取預覽因為無效的資源組態而失敗。

```
{
    "account": "111122223333",
    "detail": {
        "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb",
        "configuredResources": [
            "arn:aws:s3:::amzn-s3-demo-bucket"
        ],
        "createdAt": "2020-02-20T00:00:00.00Z",
        "previousStatus": "CREATING",
        "region": "us-west-2",
        "status": "FAILED",
        "statusReason": {
            "code": "INVALID_CONFIGURATION"
        },
        "version": "1.0"
    },
    "detail-type": "Access Preview State Change",
    "id": "99998888-7777-6666-5555-444433332222",
    "region": "us-west-2",
    "resources": [
        "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/MyAnalyzer"
    ],
    "source": "aws.access-analyzer",
    "time": "2020-02-20T00:00:00.00Z",
    "version": "0"
}
```

## 使用主控台建立事件規則
<a name="access-analyzer-create-rule"></a>

下列程序說明如何使用主控台建立事件規則。

1. 前往 [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/) 開啟 Amazon EventBridge 主控台。

1. 使用下列值，建立 EventBridge 規則，以監視問題清單事件或存取預覽事件：
   + 針對**規則類型**，選擇**具有事件模式的規則**。
   + 在 **Event source** (事件來源) 中，選擇 **Other** (其他)。
   + 在 **Event pattern** (事件模式) 中，選擇 **Custom patterns (JSON editor)** (自訂模式 (JSON 編輯器))，並將下列事件模式範例貼到文字區域：
     + 若要根據任意 IAM Access Analyzer 事件建立規則，請使用下列模式範例：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ]
       }
       ```
     + 若要根據外部存取、內部存取或未使用的存取調查結果事件建立規則，請依照下列模式範例操作：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ],
         "detail-type": [
           "Access Analyzer Finding",
           "Internal Access Finding",
           "Unused Access Finding for IAM entities"
         ]
       }
       ```
     + 若要僅根據外部存取權調查結果事件建立規則，請使用下列模式範例：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ],
         "detail-type": [
           "Access Analyzer Finding"
         ]
       }
       ```
     + 若要僅根據內部存取調查結果事件建立規則，請依照下列模式範例操作：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ],
         "detail-type": [
           "Internal Access Finding"
         ]
       }
       ```
     + 若要僅根據未使用的存取權調查結果事件建立規則，請使用下列模式範例：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ],
         "detail-type": [
           "Unused Access Finding for IAM entities"
         ]
       }
       ```
     + 若要根據存取預覽事件建立規則，請使用下列模式範例：

       ```
       {
         "source": [
           "aws.access-analyzer"
         ],
         "detail-type": [
           "Access Preview State Change"
         ]
       }
       ```
   + 對於**目標類型**，請選擇**AWS 服務**，對於**選取目標**，選擇目標，例如 Amazon SNS 主題或 AWS Lambda 函數。當接收到符合規則中定義之事件模式的事件時，就會觸發目標。

   如需有關建立規則的詳細資訊，請參閱 *Amazon EventBridge User Guide* 中的 [Creating Amazon EventBridge rules that react to events](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule.html)。

### 使用 CLI 建立事件規則
<a name="access-analyzer-create-rule-cli"></a>

1. 使用下列項目來使用 AWS CLI為 Amazon EventBridge 建立規則。將規則名稱 *TestRule* 取代為您的規則名稱。

   ```
   aws events put-rule --name TestRule --event-pattern "{\"source\":[\"aws.access-analyzer\"]}"
   ```

1. 您可以自訂規則，只針對產生的一部分問題清單 (例如具有特定屬性的問題清單) 觸發目標動作。下列範例示範如何建立規則，該規則會僅針對狀態為作用中的問題清單而觸發目標動作。

   ```
   aws events put-rule --name TestRule --event-pattern "{\"source\":[\"aws.access-analyzer\"],\"detail-type\":[\"Access Analyzer Finding\"],\"detail\":{\"status\":[\"ACTIVE\"]}}"
   ```

   下列範例示範如何建立規則，該規則會僅針對狀態從 `Creating` 變更為 `Completed` 的存取預覽而觸發目標動作。

   ```
   aws events put-rule --name TestRule --event-pattern "{\"source\":[\"aws.access-analyzer\"],\"detail-type\":[\"Access Preview State Change\"],\"detail\":{\"status\":[\"COMPLETED\"]}}"
   ```

1. 若要將 Lambda 函數定義為您建立之規則的目標，請使用下列範例命令。將 ARN 中的區域和函數名稱取代為適合您環境的值。

   ```
   aws events put-targets --rule TestRule --targets Id=1,Arn=arn:aws:lambda:us-east-1:111122223333:function:MyFunction
   ```

1. 新增叫用規則目標所需的許可。下列範例示範在遵循上述範例後如何將許可授與 Lambda 函數。

   ```
   aws lambda add-permission --function-name MyFunction --statement-id 1 --action 'lambda:InvokeFunction' --principal events.amazonaws.com
   ```