この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
こんにちは、福岡オフィス所属、IT推進室の井手です。
re:Invent 2018にて、Amazon CloudWatch Logs Insightsという機能追加が発表されました。 Amazon CloudWatch Logs Insightsでログの高速な分析が可能になりました #reinvent
先日のブログでコンソールは試したので今回は、CLIで使ってみたのでご紹介します。
準備
まずは準備です。ドキュメントを確認しようと、CloudWatchのCLIリファレンスを確認しましたが見当たらなく、あれ?っと思ったのですがLogsのCLIリファレンスにCloudWatch Logs Insights関係のコマンドの記載がありました。(内容が簡単なので実はここが今回1番伝えたかったりしますw) どうやら現状、以下のコマンドが用意されているようです。
コマンド名 | 機能 |
---|---|
start-query | queryの実行 |
stop-query | queryの停止 |
get-query-results | query結果の取得 |
手頃なログとして、Lambdaのテンプレートより「hello-world-python3」を利用します。
テンプレートに不要な部分を削除しテストを実行しログをCLIで呼び出してみます。
やってみた
テスト結果がロググループに出力されている事を確認し以下のコマンドを実行します。
aws logs start-query \
--log-group-name '/aws/lambda/hello-world-insight' \
--start-time 1546009200 \
--end-time 1546020000 \
--query-string 'fields @timestamp, @message
| sort @timestamp desc
| limit 4'
Output
{
"queryId": "01251c78-7e7b-4c8b-872d-0864390183fs"
}
queryIdが取れたので、次にこれを利用して結果を取得します。
aws logs get-query-results \
--query-id '01251c78-7e7b-4c8b-872d-0864390183fs'
Output(不要な@ptrは消しています)
{
"results": [
[
{
"field": "@timestamp",
"value": "2018-12-28 17:59:16.155"
},
{
"field": "@message",
"value": "REPORT RequestId: 3a67f8af-0aca-11e9-be16-c16d11edadca\tDuration: 0.41 ms\tBilled Duration: 100 ms \tMemory Size: 128 MB\tMax Memory Used: 20 MB\t\n"
}
],
[
{
"field": "@timestamp",
"value": "2018-12-28 17:59:16.155"
},
{
"field": "@message",
"value": "END RequestId: 3a67f8af-0aca-11e9-be16-c16d11edadca\n"
}
],
[
{
"field": "@timestamp",
"value": "2018-12-28 17:59:16.154"
},
{
"field": "@message",
"value": "value1 = Hello,World!!\n"
}
],
[
{
"field": "@timestamp",
"value": "2018-12-28 17:59:16.154"
},
{
"field": "@message",
"value": "START RequestId: 3a67f8af-0aca-11e9-be16-c16d11edadca Version: $LATEST\n"
}
]
],
"statistics": {
"recordsMatched": 18.0,
"recordsScanned": 18.0,
"bytesScanned": 1504.0
},
"status": "Complete"
}
さいごに
今回CLIを試しましたが、シンプルな内容でドキュメントを読めばすぐに試せる内容かと思います。どなたかの参考になれば幸いです。