0
1

More than 3 years have passed since last update.

S3 SELECTでCloudTrail証跡オブジェクトを検索してみる

Last updated at Posted at 2021-02-10

S3 SELECTを使ってCloudTrailの証跡オブジェクト(JSONでS3に吐かれる)をクエリーする必要に迫られたが、意外にサンプルがなかったのでまとめておく。

やりたいこと

  • CloudTrailのファイルの中身をサクッと見たい。
  • ダウンロードはしたくない。Athenaを準備するほどでもない。

S3 SELECTの入出力設定

カテゴリー 項目
入力設定 フォーマット JSON
JSONコンテンツタイプ
圧縮 GZIP
出力設定 フォーマット JSON

サンプルクエリー

300行取得

SELECT * FROM s3object s LIMIT 300
指定したイベント情報を取得
SELECT s.eventTime, s.sourceIPAddress, s.userIdentity.arn, s.awsRegion, s.eventName, s.eventSource, s.eventType,s.eventCategory, s.errorCode, s.errorMessage FROM s3object[*].Records[*] s LIMIT 10
列名に別名を指定
SELECT s.eventTime as EventTime, s.sourceIPAddress as SourceIP, s.userIdentity.arn as UserID, s.userIdentity.sessionContext.attributes.mfaAuthenticated as MFA, s.eventName as EventName, s.eventSource as EventSource, s.errorCode as ErrorCode, s.errorMessage as ErrorMessage FROM s3object[*].Records[*] s LIMIT 10
特定のイベントソースのみフィルタリング
SELECT * FROM s3object[*].Records[*] s WHERE s.eventSource = 'ec2.amazonaws.com' AND s.eventName != 'DescribeInstances'
イベントを一覧表示して全体を把握
SELECT s.eventTime as EventTime, s.eventName as EventName, s.eventSource as EventSource FROM s3object[*].Records[*] s
特定のイベントソースのエラーの一覧化
SELECT s.eventTime as EventTime, s.sourceIPAddress as SourceIP, s.userIdentity.arn as UserID, s.errorCode as ErrorCode, s.errorMessage as ErrorMessage, s.eventName as EventName, s.eventSource as EventSource FROM s3object[*].Records[*] s WHERE s.eventSource = 'kinesis.amazonaws.com'
特定イベントの件数集計
SELECT count(s.eventName) as DynamoDBEventCount FROM s3object[*].Records[*] s WHERE s.eventSource = 'dynamodb.amazonaws.com'

メモ

  • 改行入れるとエラーになっちゃう。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1