概要
ログ出力した時に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