0
0

More than 1 year has passed since last update.

CloudWatchのログでメールアドレスをキーに検索する

Posted at

「Aws::CloudWatchLogs::Errors::InvalidParameterException (Invalid filter pattern)」って出るだけで、なんかうまくいかないなーと思ったんですが、なんてことはなくて、コンソールで「"test@sample.co.jp"」とするように、「"」で囲むだけで出来ました。

これより難しいfilter_patternの書き方については色々情報が出てくるんですが、こういう初歩的な情報が思うように見つからず(もしくは読み解けず)結構苦労しました。

start_time、end_timeの方も微妙に詰まって、「UNIX時間のミリ秒」と書いてあったのを「秒」で指定していただけでした。
1000倍すればよかったんですね。

以下、コードはrubyです。

    client = Aws::CloudWatchLogs::Client.new({credentials: Aws::Credentials.new(API_KEY, API_SECRET)})
    resp = client.filter_log_events({
      log_group_name: "/path/to/log_group",
      start_time: 1659884400000, # 2022/8/8 00:00:00のJST
      end_time: 1659920400000, # 2022/8/8 10:00:00のJST
      filter_pattern: '"test@sample.co.jp"',
      limit: 10,
    })
    if resp.events.length == 0 
      p 'ログなし'
    else 
      resp.events.each do |r|
        # ログの本文を表示
        p r.message
      end
    end  

参考URL

公式のAPI文書:https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_FilterLogEvents.html
日付→UNIX時間の変換:https://keisan.casio.jp/exec/system/1526003938

0
0
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
0