我如何解决使用 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 年前