CloudWatch Logs Insights で CloudTrail ログを取得および分析する方法を教えてください。
Amazon CloudWatch Logs Insights を使用して AWS CloudTrail ログをクエリおよび分析したいです。
簡単な説明
CloudTrail が CloudWatch Logs ロググループにログを送信するように設定した後、そのロググループに対してクエリを実行できます。API アクティビティをイベントタイプ、ユーザーアイデンティティ、AWS サービス、または時間範囲でフィルタリングできます。
この記事では、CloudWatch Logs を取得して Amazon S3 バケットとオブジェクトのアクティビティを分析および調査するためのサンプルクエリを提供します。
**注: ** デフォルトでは、CloudTrail は Amazon Simple Storage Service (Amazon S3) のデータイベントを記録しません。Amazon S3 バケットとオブジェクトのイベントログを取得するには、CloudTrail でイベントロギングを有効にする必要があります。
解決策
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 データイベントを有効にする必要があります。手順については、『データイベントをログ記録する』を参照してください。
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) ID タイプでイベントを集約するには、次のクエリを実行してください。
#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 コールをフィルタリングし、イベントを 1 時間単位のビンに集約するには、次のクエリを実行してください。
#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 |
関連情報
This article was reviewed and updated on 2026-05-25.
関連するコンテンツ
質問済み 2年前
