如何對使用 Athena 查詢 CloudTrail 資料時的逾時問題進行疑難排解?

3 分的閱讀內容
0

當我使用 Amazon Athena 查詢 AWS CloudTrail 資料時,查詢的執行時間長或發生逾時。

解決方法

CloudTrail 日誌的大小會隨著時間而增加,即使您對 CloudTrail 表進行分割以減少查詢的執行時間也是如此。針對高度分割的表格進行查詢的規劃時間較長,而且無法快速完成。

若要解決逾時問題,您可以使用分割區投影手動建立 CloudTrail 表。這可讓 Athena 動態計算 CloudTrail 表的值,而不需要掃描大量的分割區。使用分割區投影,您不需要管理分割區,因為分割區值和位置是根據組態計算的。

若要依分割區投影的時間戳記建立 CloudTrail 資料表,請參閱使用分割區投影在 Athena 中為 CloudTrail 日誌建立資料表

若要按分割區投影的年、月和日為多個帳戶建立 CloudTrail 表,請使用類似下列內容的命令:

CREATE EXTERNAL TABLE ctrail_pp_ymd (
eventversion STRING,
useridentity STRUCT<
               type:STRING,
               principalid:STRING,
               arn:STRING,
               accountid:STRING,
               invokedby:STRING,
               accesskeyid:STRING,
               userName:STRING,
sessioncontext:STRUCT<
attributes:STRUCT<
               mfaauthenticated:STRING,
               creationdate:STRING>,
sessionissuer:STRUCT<
               type:STRING,
               principalId:STRING,
               arn:STRING,
               accountId:STRING,
               userName:STRING>>>,
eventtime STRING,
eventsource STRING,
eventname STRING,
awsregion STRING,
sourceipaddress STRING,
useragent STRING,
errorcode STRING,
errormessage STRING,
requestparameters STRING,
responseelements STRING,
additionaleventdata STRING,
requestid STRING,
eventid STRING,
resources ARRAY<STRUCT<
               ARN:STRING,
               accountId:STRING,
               type:STRING>>,
eventtype STRING,
apiversion STRING,
readonly STRING,
recipientaccountid STRING,
serviceeventdetails STRING,
sharedeventid STRING,
vpcendpointid STRING
)
PARTITIONED BY (account string, region string, year string, month string, day string)
ROW FORMAT SERDE
  'com.amazon.emr.hive.serde.CloudTrailSerde'
STORED AS INPUTFORMAT
  'com.amazon.emr.cloudtrail.CloudTrailInputFormat'
OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://doc_example_bucket/AWSLogs/'
TBLPROPERTIES (
  'projection.enabled'='true',
  'projection.day.type'='integer',
  'projection.day.range'='01,31',
  'projection.day.digits'='2',
  'projection.month.type'='integer',
  'projection.month.range'='01,12',
  'projection.month.digits'='2',
  'projection.region.type'='enum',
  'projection.region.values'='us-east-1,us-east-2,us-west-2',
  'projection.year.type'='integer',
  'projection.year.range'='2015,2021',
  'projection.account.type'='enum',
  'projection.account.values'='111122223334444,5555666677778888',  
  'storage.location.template'='s3://doc_example_bucket/AWSLogs/${account}/CloudTrail/${region}/${year}/${month}/${day}'
)

請務必在前面的查詢中取代下列項目:

  • ctrail_pp_ymd 取代為 CloudTrail 表的名稱
  • doc_example_bucket 取代為您要在其中建立 CloudTrail 表的 Amazon Simple Storage Service (Amazon S3) 儲存貯體之名稱
  • 11112222333344445555666677778888 取代為您要為其建立 CloudTrail 表的帳戶 ID
  • us-east-1,us-east-2,us-west-2 取代為您要為其建立 CloudTrail 表的區域
  • 表格屬性基於您的使用情況
  • 投影範圍基於您的使用情況 (例如,如果您的 CloudTrail 資料只能從 2018 年開始使用,則將分割區欄的投影範圍取代為 「2018,2021」)

若要為同一組織下的多個帳戶建立 CloudTrail 表,請使用類似下列的命令:

CREATE EXTERNAL TABLE ctrail_pp_ymd_org (
eventversion STRING,
useridentity STRUCT<
               type:STRING,
               principalid:STRING,
               arn:STRING,
               accountid:STRING,
               invokedby:STRING,
               accesskeyid:STRING,
               userName:STRING,
sessioncontext:STRUCT<
attributes:STRUCT<
               mfaauthenticated:STRING,
               creationdate:STRING>,
sessionissuer:STRUCT<
               type:STRING,
               principalId:STRING,
               arn:STRING,
               accountId:STRING,
               userName:STRING>>>,
eventtime STRING,
eventsource STRING,
eventname STRING,
awsregion STRING,
sourceipaddress STRING,
useragent STRING,
errorcode STRING,
errormessage STRING,
requestparameters STRING,
responseelements STRING,
additionaleventdata STRING,
requestid STRING,
eventid STRING,
resources ARRAY<STRUCT<
               ARN:STRING,
               accountId:STRING,
               type:STRING>>,
eventtype STRING,
apiversion STRING,
readonly STRING,
recipientaccountid STRING,
serviceeventdetails STRING,
sharedeventid STRING,
vpcendpointid STRING
)
PARTITIONED BY (account string, region string, year string, month string, day string)
ROW FORMAT SERDE
  'com.amazon.emr.hive.serde.CloudTrailSerde'
STORED AS INPUTFORMAT
  'com.amazon.emr.cloudtrail.CloudTrailInputFormat'
OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://doc_example_bucket/AWSLogs/doc_example_orgID/'
TBLPROPERTIES (
  'projection.enabled'='true',
  'projection.day.type'='integer',
  'projection.day.range'='01,31',
  'projection.day.digits'='2',
  'projection.month.type'='integer',
  'projection.month.range'='01,12',
  'projection.month.digits'='2',
  'projection.region.type'='enum',
  'projection.region.values'='us-east-1,us-east-2,us-west-2',
  'projection.year.type'='integer',
  'projection.year.range'='2010,2100',
  'projection.account.type'='enum',
  'projection.account.values'='111122223334444,5555666677778888',  
  'storage.location.template'='s3://doc_example_bucket/AWSLogs/doc_example_orgID/${account}/CloudTrail/${region}/${year}/${month}/${day}'
)

**注意:**如果您需要查詢 2010 年之前的 CloudTrail 資料,請務必更新 projection.year.range 中的年份範圍。

確保在上述查詢中替換下列內容:

  • ctrail_pp_ymd_org 取代為 CloudTrail 表的名稱
  • doc_example_bucket 取代為您要在其中建立 CloudTrail 表的 Amazon S3 儲存貯體之名稱
  • doc_example_orgID 取代為您的組織 ID
  • 11112222333344445555666677778888 取代為您要為其建立 CloudTrail 表的帳戶 ID
  • us-east-1us-east-2us-west-2 取代為您要為其建立 CloudTrail 表的區域
  • 表格屬性基於您的使用情況
  • 投影範圍基於您的使用情況 (例如,如果您的 CloudTrail 資料只能從 2018 年開始使用,則將分割區欄的投影範圍取代為 「2018,2021」)

在執行查詢時,請務必在查詢中包含分割區欄上的限制條件。這可讓 Athena 掃描較少的資料,加快查詢處理速度。

例如,您可以執行類似下列的命令,找出哪個使用者向 S3 儲存貯體發出了 GetObject 請求。此查詢中的表格使用年、月和日分割策略。

**注意:**請務必在 CloudTrail 中為 Amazon S3 啟用事件日誌記錄

SELECT useridentity.arn, eventtime  FROM "ctrail_pp_ymd"
where eventname = 'GetObject'
and year = '2021'
and month = '05'
and region = 'us-east-1'
and cast(json_extract(requestparameters, '$.bucketName')as varchar) ='doc_example_bucket'

確保在上述查詢中替換下列內容:

  • ctrail_pp_ymd 取代為 CloudTrail 表的名稱
  • doc_example_bucket 取代為您要在其中建立 CloudTrail 表的 S3 儲存貯體之名稱
  • 限制條件基於您的使用情況

如果您在執行上述步驟後仍有逾時問題,則可以要求增加服務配額


相關資訊

查詢 AWS CloudTrail 日誌

AWS 官方
AWS 官方已更新 3 年前