如何使用 CloudWatch Logs Insights 检索并分析我的 CloudTrail 日志?
我想使用 Amazon CloudWatch Logs Insights 来查询并分析我的 AWS CloudTrail 日志。
简述
将 CloudTrail 配置为向 CloudWatch Logs 日志组发送日志后,您可以对该日志组运行查询。您可以按事件类型、用户身份、AWS 服务或时间范围筛选 API 活动。
本文提供检索 CloudWatch Logs 日志的示例查询,以分析和探索 Amazon S3 存储桶和对象活动。
注意: 默认情况下,CloudTrail 不捕获 Amazon Simple Storage Service (Amazon S3) 数据事件。您必须在 CloudTrail 中启用事件日志记录才能检索 Amazon S3 存储桶和对象的事件日志。
解决方法
使用 CloudWatch Logs Insights 查询 CloudTrail 日志
使用 CloudWatch 控制台选择接收您 CloudTrail 日志的日志组。
您可以在以下示例查询的基础上创建其他更复杂的 Logs Insights 查询,以满足您使用案例的需求。您还可以将查询与 CloudWatch 控制面板集成,将您的查询转化为可视化图表和图形以及相关的指标。
检索最新事件
要使用默认 @timestamp 和 @message 字段检索最近的 CloudTrail 日志事件,请运行以下示例查询:
#Retrieve the most recent CloudTrail events fields @timestamp, @message | sort @timestamp desc | limit 2
隔离单个字段
要隔离 @message 字段中的单个字段并在 CloudTrail 事件中显示所选字段,请运行以下示例查询:
#Breakout Individual Fields fields @timestamp, awsRegion, eventCategory, eventSource, eventName, eventType, sourceIPAddress, userIdentity.type | sort @timestamp desc | limit 2
该查询返回诸如 @timestamp、awsRegion、eventCategory、eventSource、eventName、eventType、sourceIPAddress 和 userIdentity.type 等列。
按 Amazon EC2 RunInstances 筛选
运行以下查询,以便根据 RunInstances API 调用检索在 AWS 账户中启动的最新 Amazon Elastic Compute Cloud (Amazon EC2) 实例:
#EC2: Recently Launched Instances fields eventTime, eventName as API, responseElements.instancesSet.items.0.instanceId as InstanceID, userIdentity.sessionContext.sessionIssuer.type as IssuerType, userIdentity.type as IdentityType, userIdentity.sessionContext.sessionIssuer.userName as userName | filter eventName = 'RunInstances' | sort eventTime desc | limit 2
输出示例:
| eventTime | API | InstanceID | IssuerType | IdentityType | userName |
| 2022-02-18T17:36:38Z | RunInstances | i-0325b4d6ae4e93c75 | Role | AssumedRole | AWSServiceRoleForAutoScaling |
按最新控制台登录筛选
运行以下查询,以便根据 ConsoleLogin API 调用检索最新控制台登录,并通过重命名字段来使用更有意义的标签:
#Console Login: Most Recent API Calls fields eventTime, eventName, responseElements.ConsoleLogin as Response, userIdentity.arn as ARN, userIdentity.type as User_Type | filter eventName = 'ConsoleLogin' | sort eventTime desc | limit 10
输出示例:
| eventTime | eventName | Response | User | User_Type |
| 2022-02-18T17:35:44Z | ConsoleLogin | Success | arn:aws:iam::123456789012:user/test_user | IAMUser |
按身份验证失败的控制台登录筛选
运行以下查询,检索不成功的控制台登录,并通过重命名字段来使用更有意义的标签:
#ConsoleLogin: Filter on Failed Logins fields eventTime, eventName, responseElements.ConsoleLogin as Response, userIdentity.userName as User, userIdentity.type as User_Type, sourceIPAddress, errorMessage | filter eventName = 'ConsoleLogin' and responseElements.ConsoleLogin = 'Failure' | sort eventTime desc | limit 10
输出示例:
| eventTime | eventName | Response | User | User_Type | sourceIPAddress | errorMessage |
| 2022-02-18T20:10:55Z | ConsoleLogin | Failure | echo | IAMUser | 12.34.56.89 | Failed authentication |
按上传到特定存储桶的 S3 对象筛选
运行以下查询,检索对存储桶的 PutObject API 调用,并通过重命名字段来使用更有意义的标签:
#Filter PutObject API Calls on a specific S3 Bucket fields @timestamp, eventName as API, requestParameters.bucketName as BucketName, requestParameters.key as Key, userIdentity.sessionContext.sessionIssuer.userName as UserName | filter eventName = 'PutObject' and BucketName = 'target-s3-bucket' | sort @timestamp desc | limit 2
注意: 将 target-s3-bucket 替换为您的存储桶名称。
输出示例:
| @timestamp | API | Bucket_Name | Key | UserName |
| 2022-02-12 17:16:07.415 | PutObject | test_bucket1 | w4r9Hg4V7g.jpg |
注意: S3 对象级事件要求您为 S3 启用 CloudTrail 数据事件。有关说明,请参阅 Logging data events(记录数据事件)。
汇总 S3 活动
运行以下查询,筛选来自 S3 服务的事件,按数量聚合事件,并按 API 调用、存储桶名称和密钥为结果分组:
#S3 Activity: Bucket Key Details filter eventSource = 's3.amazonaws.com' | stats count(*) as Hits by eventName as API, requestParameters.bucketName as BucketName, requestParameters.key as Key | sort Hits desc | limit 5
输出示例:
| API | BucketName | Key | Hits |
| ListAccessPoints | 44 | ||
| GetBucketAcl | team1-ctrail-multi-region | 27 | |
| GetObject | devsupport-prod | rdscr/individual/123456789012 | 18 |
汇总 AWS KMS 解密活动
运行以下查询,筛选来自 AWS Key Management Service (AWS KMS) Decrypt API 的事件,并按 AWS KMS 密钥和用户聚合事件:
#KMS Decrypt Activity: Key User Details fields resources.0.ARN as KMS_Key, userIdentity.sessionContext.sessionIssuer.userName as User | filter eventSource='kms.amazonaws.com' and eventName='Decrypt' | stats count(*) as Hits by KMS_Key, User | sort Hits desc | limit 2
输出示例:
| KMS_Key | User | Hits |
| arn:aws:kms:us-east-1:123456789012:key/03f2923d-e213-439d-92cf-cbb444bd85bd | AWSServiceRoleForConfig | 12 |
汇总有错误的 API 调用
运行以下查询,筛选所有包含错误代码的事件,按数量聚合事件,并按 AWS 服务、API 调用和错误代码为结果分组:
#Summarize API Calls with Errors filter ispresent(errorCode) | stats count(*) as Num_of_Events by eventSource as AWS_Service, eventName as API, errorCode | sort Num_of_Events desc | limit 5
输出示例:
| AWS_Service | API | errorCode | Num_of_Events |
| s3.amazonaws.com | GetBucketPublicAccessBlock | NoSuchPublicAccessBlockConfiguration | 79 |
汇总有错误代码的 S3 API 调用
运行以下查询,筛选包含错误代码的 S3 事件,并按错误代码和错误消息聚合事件:
#S3: Summarize Error Codes filter eventSource = 's3.amazonaws.com' and ispresent(errorCode) | stats count(*) as Hits by errorCode, errorMessage | sort Hits desc | limit 5
输出示例:
| errorCode | errorMessage | Hits |
| AccessDenied | Access Denied | 86 |
| NoSuchBucketPolicy | The bucket policy does not exist | 80 |
汇总“AccessDenied”和“UnauthorizedOperation”API 调用
运行查询,筛选带有“AccessDenied”或“UnauthorizedOperation”错误代码的事件,并按 AWS 服务、API 调用和 AWS Identity and Access Management (IAM) 身份类型聚合事件:
#Summarize AccessDenied/UnauthorizedOperation API Calls by AWS Service, API, IAM User filter (errorCode='AccessDenied' or errorCode='UnauthorizedOperation') | stats count(*) as NumberOfEvents by errorCode, eventSource as AWS_Service, eventName as API, userIdentity.type as IdentityType, userIdentity.invokedBy as InvokedBy | sort NumberOfEvents desc | limit 10
输出示例:
| errorCode | AWS Service | API | IdentityType | InvokedBy | NumberOfEvents |
| AccessDenied | s3.amazonaws.com | HeadBucket | AWSService | delivery.logs.amazonaws.com | 83 |
汇总 AWS KMS 每小时解密调用量
运行以下查询,筛选 AWS KMS Decrypt API 调用并将事件聚合到一小时的时间桶中。
#KMS: Hourly Decrypt Call Volume filter eventSource='kms.amazonaws.com' and eventName='Decrypt' | stats count(*) as Hits by bin(1h)
输出示例:
| bin(1h) | Hits |
| 2022-02-18 19:00:00.000 | 16 |
| 2022-02-18 18:00:00.000 | 25 |
相关信息
Monitor AWS CloudTrail log data in Amazon CloudWatch(视频:监控 Amazon CloudWatch 中的 AWS CloudTrail 日志数据)
Add a query to dashboard or export query results(将查询添加到控制面板或导出查询结果)
This article was reviewed and updated on 2026-05-25.
