

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用将跟踪事件导入事件数据存储中 AWS CLI
<a name="lake-cli-import-trail-events"></a>

本节介绍如何通过运行 [https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/create-event-data-store.html](https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/create-event-data-store.html) 命令来创建和配置事件数据存储，以及如何使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/start-import.html](https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/start-import.html) 命令将事件导入该事件数据存储。有关导入跟踪事件的更多信息，请参阅[将跟踪事件复制到事件数据存储](cloudtrail-copy-trail-to-lake-eds.md)。

## 正在准备导入跟踪事件
<a name="lake-cli-import-trail-events-prereqs"></a>

在导入跟踪事件之前，请做好以下准备工作。
+ 确保您的角色具有将跟踪事件导入事件数据存储的[所需权限](cloudtrail-copy-trail-to-lake-eds.md#copy-trail-events-permissions-iam)。
+ 确定您要为事件数据存储指定的 [--billing-mode](cloudtrail-lake-manage-costs.md#cloudtrail-lake-manage-costs-pricing-option) 值。`--billing-mode` 决定了摄取和存储事件的成本，以及事件数据存储的默认和最长保留期。

  将跟踪事件导入 CloudTrail Lake 时， CloudTrail 解压缩以 gzip（压缩）格式存储的日志。然后 CloudTrail 将日志中包含的事件复制到您的事件数据存储中。未压缩数据的大小可能大于 Amazon S3 的实际存储大小。要对未压缩数据的大小进行总体估计，将 S3 存储桶中日志的大小乘以 10。您可以使用此估算值为您的应用场景选择 `--billing-mode` 值。
+ 确定要为指定的值`--retention-period`。 CloudTrail 如果事件早于指定的保留期`eventTime`，则不会复制该事件。

  要确定适当的保留期，请计算要复制的最早事件（以天为单位）和要将事件在事件数据存储中保留的天数之和，如以下公式所示：

  **保留期限** = *oldest-event-in-days* \$1 *number-days-to-retain*

  例如，如果您要复制的最早事件已有 45 天，并且您想将事件在事件数据存储中再保留 45 天，则可以将保留期设置为 90 天。
+ 决定是否要使用事件数据存储来分析任何未来的事件。如果您不想摄取任何未来事件，请在创建事件数据存储时包含 `--no-start-ingestion` 参数。默认情况下，事件数据存储会在创建事件时开始摄取事件。

## 要创建事件数据存储并将跟踪事件导入该事件数据存储
<a name="lake-cli-import-trail-events-proc"></a>

1. 运行 **create-event-data-store** 命令以创建新的事件数据存储。在此示例中，`--retention-period` 被设置为 `120` 是因为要复制的最早事件已有 90 天了，且我们希望将这些事件保留 30 天。之所以设置 `--no-start-ingestion` 参数，是因为我们不想摄取任何未来事件。在此示例中，`--billing-mode` 未设置，是因为我们使用的是默认值 `EXTENDABLE_RETENTION_PRICING`，因为我们预计摄取的事件数据少于 25TB。
**注意**  
如果您正在创建事件数据存储来替换跟踪，我们建议将 `--advanced-event-selectors` 配置为与您的跟踪的事件选择器相匹配，以确保事件覆盖范围相同。默认情况下，事件数据存储会记录所有的管理事件。

   ```
   aws cloudtrail create-event-data-store  --name import-trail-eds  --retention-period 120 --no-start-ingestion
   ```

   以下为响应示例：

   ```
   {
       "EventDataStoreArn": "arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLEa-4357-45cd-bce5-17ec652719d9",
       "Name": "import-trail-eds",
       "Status": "CREATED",
       "AdvancedEventSelectors": [
           {
               "Name": "Default management events",
               "FieldSelectors": [
                   {
                       "Field": "eventCategory",
                       "Equals": [
                           "Management"
                       ]
                   }
               ]
           }
       ],
       "MultiRegionEnabled": true,
       "OrganizationEnabled": false,
       "BillingMode": "EXTENDABLE_RETENTION_PRICING",
       "RetentionPeriod": 120,
       "TerminationProtectionEnabled": true,
       "CreatedTimestamp": "2023-11-09T16:52:25.444000+00:00",
       "UpdatedTimestamp": "2023-11-09T16:52:25.569000+00:00"
   }
   ```

   初始 `Status` 为 `CREATED`，因此我们将运行 **get-event-data-store** 命令来验证摄取是否已停止。

   ```
   aws cloudtrail get-event-data-store --event-data-store eds-id
   ```

   响应显示 `Status` 现在为 `STOPPED_INGESTION`，这表明事件数据存储未摄取实时事件。

   ```
   {
       "EventDataStoreArn": "arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLEa-4357-45cd-bce5-17ec652719d9",
       "Name": "import-trail-eds",
       "Status": "STOPPED_INGESTION",
       "AdvancedEventSelectors": [
           {
               "Name": "Default management events",
               "FieldSelectors": [
                   {
                       "Field": "eventCategory",
                       "Equals": [
                           "Management"
                       ]
                   }
               ]
           }
       ],
       "MultiRegionEnabled": true,
       "OrganizationEnabled": false,
       "BillingMode": "EXTENDABLE_RETENTION_PRICING",
       "RetentionPeriod": 120,
       "TerminationProtectionEnabled": true,
       "CreatedTimestamp": "2023-11-09T16:52:25.444000+00:00",
       "UpdatedTimestamp": "2023-11-09T16:52:25.569000+00:00"
   }
   ```

1. 运行 **start-import** 命令以将跟踪事件导入步骤 1 中创建的事件数据存储。将事件数据存储的 ARN（或 ARN 的 ID 后缀）指定为 `--destinations` 参数的值。对于 `--start-event-time`，请为要复制的最早的事件指定 `eventTime`，对于 `--end-event-time`，请指定要复制的最新事件的 `eventTime`。为包含您的跟踪日志的 S3 存储桶`--import-source`指定 S3 URI、S3 存储桶的，以及用于导入跟踪事件的角色的 ARN。 AWS 区域 

   ```
   aws cloudtrail start-import \
   --destinations ["arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLEa-4357-45cd-bce5-17ec652719d9"] \
   --start-event-time 2023-08-11T16:08:12.934000+00:00 \
   --end-event-time 2023-11-09T17:08:20.705000+00:00 \
   --import-source {"S3": {"S3LocationUri": "s3://aws-cloudtrail-logs-123456789012-612ff1f6/AWSLogs/123456789012/CloudTrail/","S3BucketRegion":"us-east-1","S3BucketAccessRoleArn": "arn:aws:iam::123456789012:role/service-role/CloudTrailLake-us-east-1-copy-events-eds"}}
   ```

   以下为响应示例。

   ```
   {
      "CreatedTimestamp": "2023-11-09T17:08:20.705000+00:00",
      "Destinations": [
           "arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLEa-4357-45cd-bce5-17ec652719d9"
       ],
      "EndEventTime": "2023-11-09T17:08:20.705000+00:00",
      "ImportId": "EXAMPLEe-7be2-4658-9204-b38c3257fcd1",
      "ImportSource": { 
         "S3": { 
            "S3BucketAccessRoleArn": "arn:aws:iam::123456789012:role/service-role/CloudTrailLake-us-east-1-copy-events-eds",
            "S3BucketRegion":"us-east-1",
            "S3LocationUri": "s3://aws-cloudtrail-logs-123456789012-111ff1f6/AWSLogs/123456789012/CloudTrail/"
         }
      },
      "ImportStatus": "INITIALIZING",
      "StartEventTime": "2023-08-11T16:08:12.934000+00:00",
      "UpdatedTimestamp": "2023-11-09T17:08:20.806000+00:00"
   }
   ```

1. 运行 [https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/get-import.html](https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/get-import.html) 命令以获取有关导入的信息。

   ```
   aws cloudtrail get-import --import-id import-id
   ```

   以下为响应示例。

   ```
   {
       "ImportId": "EXAMPLEe-7be2-4658-9204-b38c3EXAMPLE",
       "Destinations": [
           "arn:aws:cloudtrail:us-east-1:123456789012:eventdatastore/EXAMPLEa-4357-45cd-bce5-17ec652719d9"
       ],
       "ImportSource": {
           "S3": {
               "S3LocationUri": "s3://aws-cloudtrail-logs-123456789012-111ff1f6/AWSLogs/123456789012/CloudTrail/",
               "S3BucketRegion":"us-east-1",
               "S3BucketAccessRoleArn": "arn:aws:iam::123456789012:role/service-role/CloudTrailLake-us-east-1-copy-events-eds"
           }
       },
       "StartEventTime": "2023-08-11T16:08:12.934000+00:00",
       "EndEventTime": "2023-11-09T17:08:20.705000+00:00",
       "ImportStatus": "COMPLETED",
       "CreatedTimestamp": "2023-11-09T17:08:20.705000+00:00",
       "ImportStatistics": {
           "PrefixesFound": 1548,
           "PrefixesCompleted": 1548,
           "FilesCompleted": 92845,
           "EventsCompleted": 577249,
           "FailedEntries": 0
       }
   }
   ```

   如果没有故障，导入完成时 `ImportStatus` 显示为 `COMPLETED`，如果出现故障，则会显示 `FAILED`。

   如果导入具有 `FailedEntries`，则可以运行 [https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/list-import-failures.html](https://docs.aws.amazon.com/cli/latest/reference/cloudtrail/list-import-failures.html) 命令以返回失败列表。

   ```
   aws cloudtrail list-import-failures --import-id import-id
   ```

   要重试失败的导入，请仅使用 `--import-id` 参数运行 **start-import** 命令。重试导入时，将在出现故障的位置 CloudTrail 恢复导入。

   ```
   aws cloudtrail start-import --import-id import-id
   ```