📝

[小ネタ] CloudTrail のイベント履歴から特定のイベント名を重複なしで抽出するコマンド

に公開

以下のコマンドで抽出可能です。

$ jq -r '.Records[].eventName' event_history.json | sort -u
AddPermission20150331
AllocateAddress
AssociateAddress
AuthorizeSecurityGroupIngress
CreateApplication

CloudTrail のイベント履歴検索について

Working with AWS CloudTrail Lake - AWS CloudTrail
CloudTrail のイベント履歴を簡便に調査できる機能としては CloudTrail Lake があります。
SQL 形式でコンソール上からイベント履歴を検索でき、一部のリージョンでは AI で自動でクエリを生成することも可能です。

Create CloudTrail Lake queries from natural language prompts - AWS CloudTrail

The query summarization feature is in preview release for CloudTrail Lake and is subject to change. This feature is available in the following regions: Asia Pacific (Tokyo), US East (N. Virginia), and US West (Oregon).

また、S3 バケットに保存されている証跡を取り込むことで 90 日以上前の記録についても検索可能です。
Copy trail events to an event data store - AWS CloudTrail

一方で、CloudTrail Lake には以下の制約や注意点があります。

  • SQL を知っている必要がある
  • 使用できる SQL に制約がある
  • AI でのクエリ生成は一部のリージョンでのみサポートされている
  • 証跡を取り込む期間によってはコストが増大する

コマンドでの履歴検索について

Downloading your CloudTrail log files - AWS CloudTrail
上記課題の中で「SQL を知っている必要がある」については、コマンドの知識でカバーできる部分があると考えています。
冒頭のコマンドもコマンドでカバーしている一例です。

手順としては以下の通りで、イベント履歴のダウンロードではコストが発生しない点もメリットです。

  1. CloudTrail コンソールでルックアップ属性や日付などである程度フィルタリング
  2. 「イベントをダウンロード」をクリックしてイベント履歴をダウンロード
  3. ローカル PC などでコマンドを使用してイベントを抽出

その他のコマンド例

  • errorCode が含まれるイベント名、エラーコード、エラーメッセージを重複なしで抽出
$ jq -r '.Records[] | select(.errorCode != null) | "\(.eventName)\t\(.errorCode)\t\(.errorMessage)"' event_history.json | sort -u
CreateConfigurationSet  ConflictException       Conflict Occurred - Reason="RESOURCE_ALREADY_EXISTS" ResourceType="configuration-set" ResourceId="test"
CreateLogStream ResourceNotFoundException       The specified log group does not exist.
CreatePool      ValidationException     The following parameters are null, empty or invalid: ['messageType', 'isoCountryCode', 'originationIdentity']
DeleteSession   InvalidStatusException  null
TerminateEnvironment    InvalidParameterValueException  An unknown error occurred
  • userAgent に sdk が含まれるイベント名とユーザーエージェントを重複なしで抽出
$ jq -r '.Records[] | select(.userAgent | contains("sdk")) | "\(.eventName)\t\(.userAgent)"' event_history.json | sort -u
RegisterManagedInstance aws-sdk-go/1.55.5 (go1.24.6; linux; amd64) amazon-ssm-agent/3.3.3050.0
UpdateInstanceInformation       aws-sdk-go/1.55.5 (go1.24.6; linux; amd64) amazon-ssm-agent/3.3.3050.0
  • sourceIPAddress とイベント名をセットで重複なしで抽出
$ jq -r '.Records[] | "\(.sourceIPAddress)\t\(.eventName)"' event_history.json | sort -u
13.115.133.120  CreateLogGroup
18.180.19.176   CreateDataChannel
18.180.19.176   OpenDataChannel
18.180.19.176   TerminateEnvironment
18.180.19.176   UpdateInstanceInformation
35.77.13.167    TerminateEnvironment
35.77.8.1       TerminateEnvironment
52.194.35.46    CreateDataChannel
52.194.35.46    OpenDataChannel
52.194.35.46    UpdateInstanceInformation
54.238.231.171  RegisterManagedInstance
54.238.231.171  UpdateInstanceInformation
AWS Internal    AddPermission20150331
AWS Internal    PutBucketNotification
AWS Internal    UpdateFunctionCode20150331v2
autoscaling.amazonaws.com       RunInstances
autoscaling.amazonaws.com       TerminateInstances
cloudformation.amazonaws.com    AllocateAddress
cloudformation.amazonaws.com    AuthorizeSecurityGroupIngress
cloudformation.amazonaws.com    CreateAutoScalingGroup
config.amazonaws.com    PutEvaluations
ec2.amazonaws.com       SharedSnapshotVolumeCreated
elasticbeanstalk.amazonaws.com  AssociateAddress
sms-voice.amazonaws.com CreateLogStream
sso.amazonaws.com       DeleteIdentityStore

まとめ

今回は CloudTrail のイベント履歴から特定のイベント名を重複なしで抽出するコマンドなどを紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion