

# 使用 Amazon EventBridge 监控 AWS Identity and Access Management Access Analyzer
<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 发送事件，其中包含每个访问预览并更改为其状态。这包括首次创建访问预览（状态为“Creating”）、访问预览完成（状态为“Completed”）或访问预览创建失败（状态为“Failed”）时的事件。要接收有关访问预览的通知，您必须在 EventBridge 中创建事件规则。在创建事件规则时，您还可以根据规则指定要触发的目标操作。例如，您可以创建一个事件规则，该规则会在从 IAM Access Analyzer 接收完成的访问预览的事件时触发 Amazon SNS 主题。

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

自您的账户中发生事件后约 1 小时内，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"
}
```

以下示例显示了发送到 EventBridge 以进行访问预览的事件数据，其状态从 `Creating` 更改为 `Completed`。在详细信息对象中，`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"
}
```

以下示例显示了发送到 EventBridge 以进行访问预览的事件数据，其状态从 `Creating` 更改为 `Failed`。在 `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 用户指南》中的[创建对事件作出反应的 Amazon EventBridge 规则](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
   ```