使用 Amazon EventBridge 监控 Amazon Identity and Access Management Access Analyzer - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon EventBridge 监控 Amazon Identity and Access Management Access Analyzer

通过本主题中的信息了解如何使用 Amazon EventBridge 监控 IAM Access Analyzer 结果以及如何访问预览。EventBridge 是 Amazon CloudWatch Events 的新版本。

结果事件

IAM Access Analyzer 针对每个生成的结果、对现有结果状态的更改以及删除结果的时间向 EventBridge 发送一个事件。要接收结果以及有关结果的通知,您必须在 Amazon EventBridge 中创建事件规则。在创建事件规则时,您还可以根据规则指定要触发的目标操作。例如,您可以创建一个事件规则,该规则会在从 IAM Access Analyzer 接收新结果的事件时触发 Amazon SNS 主题。

访问预览事件

IAM Access Analyzer 会向 EventBridge 发送事件,其中包含每个访问预览并更改为其状态。这包括首次创建访问预览(状态为“Creating”)、访问预览完成(状态为“Completed”)或访问预览创建失败(状态为“Failed”)时的事件。要接收有关访问预览的通知,您必须在 EventBridge 中创建事件规则。在创建事件规则时,您还可以根据规则指定要触发的目标操作。例如,您可以创建一个事件规则,该规则会在从 IAM Access Analyzer 接收完成的访问预览的事件时触发 Amazon SNS 主题。

事件通知频率

自您的账户中发生事件后约 1 小时内,IAM Access Analyzer 会将新结果的事件以及带状态更新的结果发送到 EventBridge。IAM Access Analyzer 还会在因保留期已过而删除已解析的结果时向 EventBridge 发送事件。对于因生成它们的分析器被删除而被删除的结果,事件将在分析器被删除后大约 24 小时发送到 EventBridge。删除结果时,不会更改其状态。相反,会将 isDeleted 属性设置为 true。IAM Access Analyzer 还会将新创建的访问预览和访问预览状态更改的事件发送到 EventBridge。

外部访问调查发现事件示例

以下是发送到 EventBridge 的 IAM Access Analyzer 外部访问调查发现事件示例。列出的 id 是 EventBridge 中事件的 ID。要了解更多信息,请参阅 EventBridge 中的事件和事件模式

detail 对象中,accountIdregion 属性的值是指结果中报告的账户和区域。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:::my-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:::my-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" }

以下是发送到 EventBridge 的 IAM Access Analyzer 未使用的访问调查发现事件示例。列出的 id 是 EventBridge 中事件的 ID。要了解更多信息,请参阅 EventBridge 中的事件和事件模式

detail 对象中,accountIdregion 属性的值是指结果中报告的账户和区域。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" } }

访问预览事件示例

以下示例显示了在您创建访问预览时发送到 EventBridge 的第一个事件的数据。resources 数组是一个单例,其中包含了访问预览所关联的分析器的 ARN。在 detail 对象中,id 是指访问预览 ID,configuredResources 是指为其创建访问预览的资源。statusCreating,指访问预览状态。previousStatus 未指定,因为访问预览刚刚创建。

{ "account": "111122223333", "detail": { "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb", "configuredResources": [ "arn:aws:s3:::example-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。statuspreviousStatus 是指访问预览状态,其中之前的状态为 Creating,当前状态为 Completed

{ "account": "111122223333", "detail": { "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb", "configuredResources": [ "arn:aws:s3:::example-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。statuspreviousStatus 是指访问预览状态,其中之前的状态为 Creating,当前状态为 FailedstatusReason 字段提供了指示访问预览由于资源配置无效而失败的原因代码。

{ "account": "111122223333", "detail": { "accessPreviewId": "aaaabbbb-cccc-dddd-eeee-ffffaaaabbbb", "configuredResources": [ "arn:aws:s3:::example-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" }

使用控制台创建事件规则

以下过程介绍了如何使用控制台创建事件规则。

  1. 访问 https://console.aws.amazon.com/events/,打开 Amazon EventBridge 控制台。

  2. 使用以下值创建监控查找事件或访问预览事件的 EventBridge 规则:

    • 对于规则类型,选择具有事件模式的规则

    • 对于 Event source(事件源),选择 Other(其他)。

    • 对于 Event pattern(事件模式),选择 Custom patterns (JSON editor) [自定义模式(JSON 编辑器)],并将以下事件模式之一粘贴到文本区域:

      • 要基于外部访问或未使用的访问调查发现事件创建规则,请使用以下模式示例:

        { "source": [ "aws.access-analyzer" ], "detail-type": [ "Access Analyzer Finding" ] }
      • 要仅基于未使用的访问调查发现事件创建规则,请使用以下模式示例:

        { "source": [ "aws.access-analyzer" ], "detail-type": [ "Unused Access Finding for IAM entities" ] }
        注意

        您不能仅基于外部访问调查发现事件创建规则。

      • 要基于访问预览事件创建规则,请使用以下模式示例:

        { "source": [ "aws.access-analyzer" ], "detail-type": [ "Access Preview State Change" ] }
    • 对于目标类型,选择 Amazon 服务,对于选择目标,选择一个目标,例如 Amazon SNS 主题或 Amazon Lambda 函数。在收到与规则中定义的事件模式匹配的事件时将触发目标。

    要详细了解关于创建规则的信息,请参阅《Amazon EventBridge 用户指南》中的创建对事件作出反应的 Amazon EventBridge 规则

使用 CLI 创建事件规则

  1. 通过以下命令为使用 Amazon CLI 的 Amazon EventBridge 创建规则。将规则名称 TestRule 替换为您的规则名称。

    aws events put-rule --name TestRule --event-pattern "{\"source\":[\"aws.access-analyzer\"]}"
  2. 您可以自定义规则以便仅针对一小部分生成的结果(例如,具有特定属性的结果)触发目标操作。以下示例演示了如何创建仅针对状态为“活动”的结果触发目标操作的规则。

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

    以下示例演示了如何创建仅针对状态从 CreatingCompleted 的访问预览触发目标操作的规则。

    aws events put-rule --name TestRule --event-pattern "{\"source\":[\"aws.access-analyzer\"],\"detail-type\":[\"Access Preview State Change\"],\"detail\":{\"status\":[\"COMPLETED\"]}}"
  3. 要将 Lambda 函数定义为您所创建规则的目标,请使用以下示例命令。根据您的环境,替换 ARN 中的区域和函数名称。

    aws events put-targets --rule TestRule --targets Id=1,Arn=arn:aws:lambda:us-east-1:111122223333:function:MyFunction
  4. 添加调用规则目标所需的权限。以下示例演示了如何按照前面的示例向 Lambda 函数授予权限。

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