

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

# 使用 檢視最近的管理事件 AWS CLI
<a name="view-cloudtrail-events-cli"></a>

您可以使用 **aws cloudtrail lookup-events** 命令為目前的 AWS 區域 查詢過去 90 天的 CloudTrail 管理事件。**aws cloudtrail lookup-events** 命令會在事件發生 AWS 區域 的 中顯示事件。

查詢支援管理事件的下列屬性：
+ AWS 存取金鑰
+ 事件 ID
+ 事件名稱
+ 事件來源
+ 唯讀
+ 資源名稱
+ Resource Type (資源類型)
+ 使用者名稱

所有屬性均為選用。

[https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudtrail/lookup-events.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudtrail/lookup-events.html) 命令包含下列選項：
+ `--max-items` *<integer>* – 要在命令輸出中傳回的總項目數。如果可用的總項目數超過指定的值，會在命令的輸出中提供 `NextToken`。若要繼續分頁，請在後續命令的 starting-token 引數中提供 `NextToken` 值。請勿在 AWS CLI外部直接使用 `NextToken` 回應元素。
+ `--start-time` *<timestamp>* – 指定只會傳回在所指定時間或之後發生的事件。如果指定的開始時間晚於指定的結束時間，則會傳回錯誤。
+ `--lookup-attributes` *<integer>* – 包含查詢屬性清單。目前，該清單只可包含一個項目。
+ `--generate-cli-skeleton` *<string>* – 將 JSON 骨架列印至標準輸出，而不傳送 API 請求。如果未提供值或值輸入，則列印可用作 `--cli-input-json` 引數的範例輸入 JSON。同樣，若提供 yaml-input，它將列印可與 `--cli-input-yaml` 搭配使用的範例輸入 YAML。如果提供值輸出，它將驗證命令輸入，並為該命令傳回範例返回 JSON。產生的 JSON 骨架在 版本之間不穩定， AWS CLI 而且在產生的 JSON 骨架中沒有回溯相容性保證。
+ `--cli-input-json` *<string>* – 從提供的 JSON 字串讀取引數。JSON 字串遵循 `--generate-cli-skeleton` 參數所提供的格式。若命令列上提供了其他引數，這些值將覆寫 JSON 提供的值。任意二進位值不可透過使用 JSON 提供的值傳遞，因為字串將依照字面意思處理。這可能不會與 `--cli-input-yaml` 參數一同指定。

如需使用 AWS 命令列界面的一般資訊，請參閱 [AWS Command Line Interface 使用者指南](https://docs.aws.amazon.com/cli/latest/userguide/)。

**Contents**
+ [先決條件](#aws-cli-prerequisites-for-aws-cloudtrail)
+ [取得命令列說明](#getting-command-line-help)
+ [查詢事件](#looking-up-events-with-the-aws-cli)
+ [指定要傳回的事件數目](#specify-the-number-of-events-to-return)
+ [依時間範圍查詢事件](#look-up-events-by-time-range)
+ [依屬性查詢事件](#look-up-events-by-attributes)
  + [屬性查詢範例](#attribute-lookup-example)
+ [指定下一頁的結果](#specify-next-page-of-lookup-results)
+ [從檔案取得 JSON 輸入](#json-input-from-file)
+ [查詢輸出欄位](#view-cloudtrail-events-cli-output-fields)

## 先決條件
<a name="aws-cli-prerequisites-for-aws-cloudtrail"></a>
+ 若要執行 AWS CLI 命令，您必須安裝 AWS CLI。如需詳細資訊，請參閱[開始使用 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)。
+ 請確定您的 AWS CLI 版本大於 1.6.6。若要驗證 CLI 版本，請在命令列上執行 **aws --version**。
+ 若要設定 帳戶 AWS 區域，以及 AWS CLI 工作階段的預設輸出格式，請使用 **aws configure**命令。如需詳細資訊，請參閱[設定 AWS 命令列界面](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)。

**注意**  
CloudTrail AWS CLI 命令區分大小寫。

## 取得命令列說明
<a name="getting-command-line-help"></a>

若要查看 `lookup-events` 的命令列說明，請輸入下列命令：

```
aws cloudtrail lookup-events help
```

## 查詢事件
<a name="looking-up-events-with-the-aws-cli"></a>

**重要**  
查詢請求的速率上限為每秒、每個帳戶、每個區域兩個。若超出此上限，則將發生限流錯誤。

若要查看十個最新的事件，請輸入下列命令：

```
aws cloudtrail lookup-events --max-items 10
```

所傳回的事件類似下列虛構範例，其已針對可讀性進行格式化：

```
{
    "NextToken": "kbOt5LlZe++mErCebpy2TgaMgmDvF1kYGFcH64JSjIbZFjsuvrSqg66b5YGssKutDYIyII4lrP4IDbeQdiObkp9YAlju3oXd12juy3CIZW8=", 
    "Events": [
        {
            "EventId": "0ebbaee4-6e67-431d-8225-ba0d81df5972", 
            "Username": "root", 
            "EventTime": 1424476529.0, 
            "CloudTrailEvent": "{
                  \"eventVersion\":\"1.02\",
                  \"userIdentity\":{
                        \"type\":\"Root\",
                        \"principalId\":\"111122223333\",
                        \"arn\":\"arn:aws:iam::111122223333:root\",
                        \"accountId\":\"111122223333\"},
                  \"eventTime\":\"2015-02-20T23:55:29Z\",
                  \"eventSource\":\"signin.amazonaws.com\",
                  \"eventName\":\"ConsoleLogin\",
                  \"awsRegion\":\"us-east-2\",
                  \"sourceIPAddress\":\"203.0.113.4\",
                  \"userAgent\":\"Mozilla/5.0\",
                  \"requestParameters\":null,
                  \"responseElements\":{\"ConsoleLogin\":\"Success\"},
                  \"additionalEventData\":{
                         \"MobileVersion\":\"No\",
                         \"LoginTo\":\"https://console.aws.amazon.com/console/home",
                         \"MFAUsed\":\"No\"},
                  \"eventID\":\"0ebbaee4-6e67-431d-8225-ba0d81df5972\",
                  \"eventType\":\"AwsApiCall\",
                  \"recipientAccountId\":\"111122223333\"}", 
            "EventName": "ConsoleLogin", 
            "Resources": []
        }
    ]
}
```

如需輸出中查詢相關欄位的說明，請參閱本文件後面的「[查詢輸出欄位](#view-cloudtrail-events-cli-output-fields)」一節。如需 CloudTrail 事件中欄位的說明，請參閱 [CloudTrail 記錄管理、資料和網路活動事件的內容](cloudtrail-event-reference-record-contents.md)。

## 指定要傳回的事件數目
<a name="specify-the-number-of-events-to-return"></a>

若要指定要傳回的事件數目，請輸入下列命令：

```
aws cloudtrail lookup-events --max-items <integer>
```

可能值為 1 到 50。以下範例會傳回一個事件。

```
aws cloudtrail lookup-events --max-items 1
```

## 依時間範圍查詢事件
<a name="look-up-events-by-time-range"></a>

過去 90 天的事件可用於查詢。若要指定時間範圍，請輸入下列命令：

```
aws cloudtrail lookup-events --start-time <timestamp> --end-time <timestamp>
```

`--start-time <timestamp>` 指定 (UTC) 只會傳回在所指定時間或之後發生的事件。如果指定的開始時間晚於指定的結束時間，則會傳回錯誤。

`--end-time <timestamp>` 指定 (UTC) 只會傳回在所指定時間或之前發生的事件。如果指定的結束時間早於指定的開始時間，則會傳回錯誤。

預設的開始時間是過去 90 天內可使用資料的最早日期。預設的結束時間是在最接近目前時間所發生之事件的時間。

所有時間戳記均會以 UTC 顯示。

## 依屬性查詢事件
<a name="look-up-events-by-attributes"></a>

若要依屬性進行篩選，請輸入下列命令：

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=<attribute>,AttributeValue=<string>
```

您只能為每個 **lookup-events** 命令指定一個屬性鍵/值對。以下是 `AttributeKey` 的有效值。值名稱區分大小寫。
+ `AccessKeyId`
+ `EventId`
+ `EventName`
+ `EventSource`
+ `ReadOnly`
+ `ResourceName`
+ `ResourceType`
+ `Username`

的長度上限為 `AttributeValue` 2000 個字元。下列字元 ('`_`'、'` `'、'`,`'、'`\\n`') 計算為 2000 個字元限制的兩個字元。

### 屬性查詢範例
<a name="attribute-lookup-example"></a>

下列範例命令會傳回 `AccessKeyId` 值為 `AKIAIOSFODNN7EXAMPLE` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAIOSFODNN7EXAMPLE
```

下列範例命令會傳回所指定 CloudTrail​ `EventId` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventId,AttributeValue=b5cc8c40-12ba-4d08-a8d9-2bceb9a3e002
```

下列範例命令會傳回 `EventName` 值為 `RunInstances` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances
```

下列範例命令會傳回 `EventSource` 值為 `iam.amazonaws.com` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventSource,AttributeValue=iam.amazonaws.com
```

下列範例命令會傳回寫入事件。不包括寫入事件，例如 `GetBucketLocation`跟 `DescribeStream`。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=ReadOnly,AttributeValue=false
```

下列範例命令會傳回 `ResourceName` 值為 `CloudTrail_CloudWatchLogs_Role` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue=CloudTrail_CloudWatchLogs_Role
```

下列範例命令會傳回 `ResourceType` 值為 `AWS::S3::Bucket` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceType,AttributeValue=AWS::S3::Bucket
```

下列範例命令會傳回 `Username` 值為 `root` 的事件。

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=Username,AttributeValue=root
```

## 指定下一頁的結果
<a name="specify-next-page-of-lookup-results"></a>

若要從 `lookup-events` 命令取得下一頁的結果，請輸入下列命令：

```
aws cloudtrail lookup-events <same parameters as previous command> --next-token=<token>
```

其中 *<token>* 的值取自先前命令輸出的第一個欄位。

當您在命令中使用 `--next-token` 時，必須使用與先前命令相同的參數。例如，假設您執行下列命令：

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=Username,AttributeValue=root
```

若要取得下一頁的結果，您的下一個命令會如下所示：

```
aws cloudtrail lookup-events --lookup-attributes AttributeKey=Username,AttributeValue=root --next-token=kbOt5LlZe++mErCebpy2TgaMgmDvF1kYGFcH64JSjIbZFjsuvrSqg66b5YGssKutDYIyII4lrP4IDbeQdiObkp9YAlju3oXd12juy3CIZW8=
```

## 從檔案取得 JSON 輸入
<a name="json-input-from-file"></a>

 AWS CLI 某些 AWS 服務的 有兩個參數 `--generate-cli-skeleton`和 `--cli-input-json`，可用來產生 JSON 範本，您可以修改並用作 `--cli-input-json` 參數的輸入。本節說明如何搭配使用這些參數與 `aws cloudtrail lookup-events`。如需更多詳細資訊，請參閱[AWS CLI 骨架和輸入檔案](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-skeleton.html)。

**從檔案取得 JSON 輸入來查詢 CloudTrail 事件**

1. 將 `lookup-events` 輸出重新導向至檔案，以建立與 `--generate-cli-skeleton` 搭配使用的輸入範本，如下列範例所示。

   ```
   aws cloudtrail lookup-events --generate-cli-skeleton > LookupEvents.txt
   ```

   所產生的範本檔案 (在此情況下為 LookupEvents.txt) 如下：

   ```
   {
       "LookupAttributes": [
           {
               "AttributeKey": "",
               "AttributeValue": ""
           }
       ],
       "StartTime": null,
       "EndTime": null,
       "MaxResults": 0,
       "NextToken": ""
   }
   ```

1. 視需要使用文字編輯器來修改 JSON。JSON 輸入只能包含所指定的值。
**重要**  
必須先從範本移除所有空白值或 null 值，才能使用它。

   下列範例指定時間範圍以及要傳回的結果數目上限。

   ```
   {
       "StartTime": "2023-11-01",
       "EndTime": "2023-12-12",
       "MaxResults": 10
   }
   ```

1. 若要使用編輯過的檔案做為輸入，請使用語法 `--cli-input-json file://`*<filename>*，如下列範例所示：

   ```
   aws cloudtrail lookup-events --cli-input-json file://LookupEvents.txt
   ```

**注意**  
您可以在與 `--cli-input-json` 相同的命令列上使用其他引數。

## 查詢輸出欄位
<a name="view-cloudtrail-events-cli-output-fields"></a>

**事件**  
根據所指定查詢屬性和時間範圍的查詢事件清單。事件清單是依時間排序，而且會先列出最新的事件。每個項目都會包含查詢請求的資訊，以及包含以字串呈現所擷取的 CloudTrail 事件。  
下列項目說明每個查詢事件中的欄位。

**CloudTrailEvent**  
包含以物件呈現所傳回事件的 JSON 字串。如需所有傳回之元素的資訊，請參閱[記錄內文內容](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-record-contents.html)。

**EventId**  
字串，包含所傳回事件的 GUID。

**EventName**  
字串，包含所傳回事件的名稱。

**EventSource**  
提出請求的 AWS 服務。

**EventTime**  
事件的日期和時間 (UNIX 時間格式)。

**資源**  
所傳回之事件所參考的資源清單。每個資源項目都會指定資源類型和資源名稱。

**ResourceName**  
字串，包含事件所參考資源的名稱。

**ResourceType**  
字串，包含事件所參考資源的類型。無法判定資源類型時，會傳回 null。

**使用者名稱**  
字串，包含所傳回事件之帳戶的使用者名稱。

**NextToken**  
字串，可從先前的 `lookup-events` 命令取得下一頁的結果。若要使用字符，參數必須與原始命令中的參數相同。如果 `NextToken` 項目未出現在輸出中，則沒有可傳回的其他結果。