

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

# 使用 將追蹤事件匯入事件資料存放區 AWS CLI
使用 匯入追蹤事件 AWS CLI

本節說明如何透過執行 [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)。

## 準備匯入追蹤事件


匯入追蹤事件之前，請進行以下準備工作。
+ 確認您的角色具有[所需的許可](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` 指定的值。如果事件的 `eventTime` 早於指定的保留期，則 CloudTrail 不會複製該事件。

  若要確定適當的保留期，請計算您要複製的最舊事件所經歷的天數，以及要將事件保留在事件資料存放區中的天數，並將兩者加總，如以下等式所示：

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

  例如，如果您要複製的最舊事件為 45 天前的事件，並希望這些事件在事件資料存放區中再保留 45 天，則可以將保留期設為 90 天。
+ 決定是否要使用事件資料存放區來分析任何未來事件。如果您不想擷取任何未來事件，請在建立事件資料存放區時包含 `--no-start-ingestion` 參數。依預設，事件資料存放區會在建立時開始擷取事件。

## 建立事件資料存放區並將追蹤事件匯入至該事件資料存放區


1. 執行 **create-event-data-store** 命令來建立新的事件資料存放區。在此範例中，`--retention-period` 設定為 `120`，因為要複製的最舊事件已超過 90 天，而且我們想要將事件保留 30 天。我們不想擷取任何未來事件，因此設定了 `--no-start-ingestion` 參數。在此範例中，我們使用的是預設值 `EXTENDABLE_RETENTION_PRICING`，因此並未設定 `--billing-mode`，這是因為我們預期擷取的事件資料少於 25 TB。
**注意**  
如果您要建立事件資料存放區來取代追蹤，建議您設定 `--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`。對於`--import-source`指定包含追蹤日誌之 S3 S3儲存貯體的 S3 URI、S3 儲存貯 AWS 區域 體的 ，以及用於匯入追蹤事件之角色的 ARN。

   ```
   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
   ```