0
0

More than 3 years have passed since last update.

error用filterでlambdaのrequestId取得

Posted at

概要

ログ出力した時にCloudWatchに出ているidの取得です。

公式に書いてあったfilter処理の情報から、ログ探って取得しました。
エラー時にインサイトとかで特定しやすいだろうと思ってのID抽出です。

※header情報なんで、lambdaでheader取得したら流用は効きそうです。

コード

    let reqestId = ""
    const ctx = host.switchToHttp()
    const request = ctx.getRequest()

    if (request.headers['x-apigateway-context']) {
      const context =  JSON.parse(decodeURIComponent(request.headers['x-apigateway-context']));
      if (context['awsRequestId']) {
        reqestId = context['awsRequestId'];
      }
    }

インサイト

fields @timestamp, @message
| sort @timestamp desc
| filter @message like 'ce3b81bb-b3d0-428e-a6b6-47a422f5dd91'
| limit 20

参考

https://nodejs.org/api/http.html#http_class_http_incomingmessage
https://maku77.github.io/js/web/encode-uri.html
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/nodejs-context.html

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