使用 AWS CLI 记录 Insights 事件 - AWS CloudTrail

使用 AWS CLI 记录 Insights 事件

您可以使用 AWS CLI 配置跟踪和事件数据存储以记录 Insights 事件。

注意

要针对 API 调用率记录 Insights 事件,跟踪或事件数据存储必须记录 write 管理事件。要针对 API 错误率记录 Insights 事件,跟踪或事件数据存储必须记录 readwrite 管理事件。

使用 AWS CLI 记录跟踪记录的 Insights 事件

要返回跟踪的当前 Insights 选择器,请运行 get-insight-selectors 命令。

aws cloudtrail get-insight-selectors --trail-name TrailName

以下示例响应显示了名为 insights-trail 的跟踪的 Insights 选择器。

{ "TrailARN": "arn:aws:cloudtrail:us-east-1:123456789012:trail/insights-trail", "InsightSelectors": [ { "InsightType": "ApiCallRateInsight" }, { "InsightType": "ApiErrorRateInsight" } ] }

如果跟踪未启用 Insights,则 get-insight-selectors 命令将返回以下错误消息:调用 GetInsightSelectors 操作时出现错误 (InsightNotEnabledException):Trail arn:aws:cloudtrail:us-east-1:123456789012:trail/trailName 未启用 Insights。Edit the trail settings to enable Insights, and then try the operation again.”

要将跟踪配置为记录 Insights 事件,请运行 put-insight-selectors 命令。以下示例说明如何配置跟踪以包含 Insights 事件。Insights 选择器值可以是 ApiCallRateInsight 和/或 ApiErrorRateInsight

aws cloudtrail put-insight-selectors --trail-name TrailName --insight-selectors '[{"InsightType": "ApiCallRateInsight"},{"InsightType": "ApiErrorRateInsight"}]'

以下结果显示为跟踪配置的 Insights 事件选择器。

{ "TrailARN": "arn:aws:cloudtrail:us-east-1:123456789012:trail/TrailName", "InsightSelectors": [ { "InsightType": "ApiErrorRateInsight" }, { "InsightType": "ApiCallRateInsight" } ] }

使用 AWS CLI 记录事件数据存储的 Insights 事件

要在事件数据存储上启用 Insights,必须有一个用于记录管理事件的源事件数据存储和一个用于记录 Insights 事件的目标事件数据存储。

要查看事件数据存储上是否启用了 Insights 事件,请运行 get-insight-selectors 命令。

aws cloudtrail get-insight-selectors --event-data-store arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLE-f852-4e8f-8bd1-bcf6cEXAMPLE

要查看事件数据存储是否被配置为接收 Insights 事件或管理事件,请运行 get-event-data-store 命令。

aws cloudtrail get-event-data-store --event-data-store arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLE-d483-5c7d-4ac2-adb5dEXAMPLE

如果某事件数据存储配置为接收 Insights 事件,则其 eventCategory 将设置为 Insight

以下过程向您展示如何创建目标和源事件数据存储,然后启用 Insights 事件。

  1. 运行 aws cloudtrail create-event-data-store 命令创建收集 Insights 事件的目标事件数据存储。eventCategory 的值必须为 Insight。将 retention-period-days 替换为您想要在事件数据存储中保留事件的天数。

    如果您使用 AWS Organizations 组织的管理账户登录,则如果要向委托管理员授予对事件数据存储的访问权限,请包含 --organization-enabled 参数。

    aws cloudtrail create-event-data-store \ --name insights-event-data-store \ --no-multi-region-enabled \ --retention-period retention-period-days \ --advanced-event-selectors '[ { "Name": "Select Insights events", "FieldSelectors": [ { "Field": "eventCategory", "Equals": ["Insight"] } ] } ]'

    以下为响应示例。

    { "Name": "insights-event-data-store", "ARN": "arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLEf852-4e8f-8bd1-bcf6cEXAMPLE", "AdvancedEventSelectors": [ { "Name": "Select Insights events", "FieldSelectors": [ { "Field": "eventCategory", "Equals": [ "Insight" ] } ] } ], "MultiRegionEnabled": false, "OrganizationEnabled": false, "BillingMode": "EXTENDABLE_RETENTION_PRICING", "RetentionPeriod": "90", "TerminationProtectionEnabled": true, "CreatedTimestamp": "2023-11-08T15:22:33.578000+00:00", "UpdatedTimestamp": "2023-11-08T15:22:33.714000+00:00" }

    您将使用响应中的 ARN(或 ARN 的 ID 后缀)作为步骤 3 中参数 --insights-destination 的值。

  2. 请运行 aws cloudtrail create-event-data-store 命令以创建记录管理事件的源事件数据存储。默认情况下,事件数据存储会记录所有的管理事件。您无需指定任何高级事件选择器即可记录所有管理事件。将 retention-period-days 替换为您想要在事件数据存储中保留事件的天数。如果您正在创建组织事件数据存储,请包括 --organization-enabled 参数。

    aws cloudtrail create-event-data-store --name source-event-data-store --retention-period retention-period-days

    以下为响应示例。

    { "EventDataStoreArn": "arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLE9952-4ab9-49c0-b788-f4f3EXAMPLE", "Name": "source-event-data-store", "Status": "CREATED", "AdvancedEventSelectors": [ { "Name": "Default management events", "FieldSelectors": [ { "Field": "eventCategory", "Equals": [ "Management" ] } ] } ], "MultiRegionEnabled": true, "OrganizationEnabled": false, "BillingMode": "EXTENDABLE_RETENTION_PRICING", "RetentionPeriod": 90, "TerminationProtectionEnabled": true, "CreatedTimestamp": "2023-11-08T15:25:35.578000+00:00", "UpdatedTimestamp": "2023-11-08T15:25:35.714000+00:00" }

    您将使用响应中的 ARN(或 ARN 的 ID 后缀)作为步骤 3 中参数 --event-data-store 的值。

  3. 请运行 put-insight-selectors 命令以启用 Insights 事件。Insights 选择器值可以是 ApiCallRateInsight 和/或 ApiErrorRateInsight。对于 --event-data-store 参数,请指定记录管理事件并将启用 Insights 的源事件数据存储的 ARN(或 ARN 的 ID 后缀)。对于 --insights-destination 参数,请指定将记录 Insights 事件的目标事件数据存储的 ARN(或 ARN 的 ID 后缀)。

    aws cloudtrail put-insight-selectors --event-data-store arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLE9952-4ab9-49c0-b788-f4f3EXAMPLE --insights-destination arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLEf852-4e8f-8bd1-bcf6cEXAMPLE --insight-selectors '[{"InsightType": "ApiCallRateInsight"},{"InsightType": "ApiErrorRateInsight"}]'

    以下结果显示为事件数据存储配置的 Insights 事件选择器。

    { "EventDataStoreARN": "arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLE9952-4ab9-49c0-b788-f4f3EXAMPLE", "InsightsDestination": "arn:aws:cloudtrail:us-east-1:111122223333:eventdatastore/EXAMPLEf852-4e8f-8bd1-bcf6cEXAMPLE", "InsightSelectors": [ { "InsightType": "ApiErrorRateInsight" }, { "InsightType": "ApiCallRateInsight" } ] }

首次在事件数据存储上启用 CloudTrail Insights 后,CloudTrail 最多可能需要 7 天,才能开始交付 Insights 事件,前提是在此期间检测到异常活动。