Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
如何在 Amazon Athena 中自動建立資料表,以便透過 AWS CloudTrail 日誌進行搜尋?
我想要搜尋大 AWS CloudTrail 日誌集。為了避免錯誤,我不想手動建立 Amazon Athena 資料表。
解決方法
使用 CloudTrail 主控台自動建立 Athena 資料表。如需詳細資訊,請參閱使用 Amazon Athena 的 AWS CloudTrail 日誌搜尋。
從 CloudTrail 日誌建立 Athena 資料表
請完成下列步驟:
- 開啟 CloudTrail console (CloudTrail 主控台)。
- 在導覽窗格中,選擇 Trails (追蹤),然後記下包含 CloudTrail 日誌的 Amazon Simple Storage Service (Amazon S3) 儲存貯體的名稱。
- 在導覽窗格中,選擇 Event history (事件記錄),然後選擇 Create Athena table (建立 Athena 資料表)。
- 在 Create a table in Amazon Athena (在 Amazon Athena 中建立資料表) 視窗中,從 Storage location (儲存位置) 功能表中選擇 S3 儲存貯體。
**注意:**若要改善特定時間範圍內的搜尋,您可以在 Athena 中對 CloudTrail 日誌使用分區投影。 - 選擇 Create table (建立資料表)。
- 選擇 Go to Athena (前往 Athena)。
**注意:**您可能會收到「您的帳戶沒有足夠的權限在 Amazon Athena 中建立資料表」錯誤。如果您收到此錯誤,請將 AmazonAthenaFullAccess 受管政策附加到您的 AWS Identity and Access Management (IAM) 角色。
對您的 CloudTrail 日誌資料表執行 Athena 查詢
請完成下列步驟:
- 開啟 Athena console (Athena 主控台)。
- 選擇 New query (新查詢),然後清除查詢範例。
- 輸入您的查詢,然後選擇 Run query (執行查詢)。
使用查詢範例
以下是您可以執行的查詢範例。
**注意:**在下列查詢中,將 your_athena_tablename 替換為 Athena 資料表的名稱,並將 access_key_id 替換為 20 個字元的存取金鑰 。您的存取金鑰通常會以 AKIA 或 ASIA 字元開頭。
若要顯示特定存取金鑰所有記錄的 AWS API 活動,請執行下列查詢:
SELECT eventTime, eventName, userIdentity.principalId FROM your_athena_tablename WHERE userIdentity.accessKeyId like 'access_key_id'
若要識別對 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體的安全群組變更,請執行下列查詢:
SELECT eventname, useridentity.username, sourceIPAddress, eventtime, requestparameters FROM your_athena_tablename WHERE (requestparameters LIKE '%sg-5887f224%' OR requestparameters LIKE '%sg-e214609e%' OR requestparameters LIKE '%eni-6c5ca5a8%') AND eventtime > '2017-02-15T00:00:00Z' ORDER BY eventtime ASC;
若要顯示過去 24 小時內的所有主控台登入,請執行下列查詢:
SELECT useridentity.username, sourceipaddress, eventtime, additionaleventdata FROM your_athena_tablename WHERE eventname = 'ConsoleLogin' AND eventtime >= '2017-02-17T00:00:00Z' AND eventtime < '2017-02-18T00:00:00Z';
若要顯示過去 24 小時內所有失敗的主控台登入嘗試,請執行下列查詢:
SELECT useridentity.username, sourceipaddress, eventtime, additionaleventdata FROM your_athena_tablename WHERE eventname = 'ConsoleLogin' AND useridentity.username = 'HIDDEN_DUE_TO_SECURITY_REASONS' AND eventtime >= '2017-02-17T00:00:00Z' AND eventtime < '2017-02-18T00:00:00Z';
若要確定 CloudTrail 每月成本增加情況,請執行下列查詢:
SELECT eventName,count(eventName) AS NumberOfChanges,eventSource FROM your_athena_tablename WHERE eventtime >= '2019-01-01T00:00:00Z' AND eventtime < '2019-01-31T00:00:00Z' GROUP BY eventName, eventSource ORDER BY NumberOfChanges DESC
**注意:**所定義的 eventtime 值只針對單一月份。若要取得多個月份的結果,請執行其他查詢。
若要識別缺少的 IAM 權限,請執行下列查詢:
SELECT COUNT(*) AS TotalEvents, useridentity.arn, eventsource, eventname, errorCode, errorMessage FROM your_athena_tablename WHERE (errorcode LIKE '%Denied%' OR errorcode LIKE '%Unauthorized%') AND eventtime >= '2019-10-28T00:00:00Z' AND eventtime < '2019-10-29T00:00:00Z' GROUP BY eventsource, eventname, errorCode, errorMessage, useridentity.arn ORDER BY eventsource, eventname
如果結果沒有顯示失敗的 API 呼叫,請增加查詢範圍:
SELECT COUNT(*) as TotalEvents, useridentity.arn, eventsource, eventname, errorCode, errorMessage FROM your_athena_tablename WHERE errorcode <> '' AND eventtime >= '2019-10-28T00:00:00Z' AND eventtime < '2019-10-29T00:00:00Z' GROUP BY eventsource, eventname, errorCode, errorMessage, useridentity.arn ORDER BY eventsource, eventname
相關資訊
相關內容
- 已提問 1 年前

