LoginSignup
15
10

More than 5 years have passed since last update.

AWS CloudWatchのログを「awslogs」を使ってTailする

Last updated at Posted at 2018-07-23

ECSやEKSのコンテナやLambdaによるログの出力先がCloudwatchログ一択になりつつある

CloudWatch.logにStream出力されたデータを、手元のターミナル(iterm2とか)でTailしたい人は多いはず。

文献が少なかったので備忘録を残す。

それは、awslogsを使う事で実現します。

インストール

## pipで入れたい人はこれ
pip install awslogs

## brewで入れたい人はこれ ★オススメです
brew install awslogs

Tailコマンドはこれ

# -w: 「Query for new log lines constantly」これがTailの指定
# awslogs get <log_group_name> <log_stream_name> -w -s 10m -G -S --timestamp

# サンプル
awslogs get MyService LambdaName/TaskName/* -w -s 10m -G -S --timestamp

ヘルプの内容 awslogs get --help

usage: awslogs [ get | groups | streams ] get [-h]
[--aws-access-key-id AWS_ACCESS_KEY_ID]
[--aws-secret-access-key AWS_SECRET_ACCESS_KEY]
[--aws-session-token AWS_SESSION_TOKEN]
[--profile AWS_PROFILE]
[--aws-region AWS_REGION]
[-f FILTER_PATTERN] [-w] [-G]
[-S] [--timestamp]
[--ingestion-time] [-s START]
[-e END] [--no-color] [-q QUERY]
[log_group_name]
[log_stream_name]

Get logs

positional arguments:
log_group_name log group name
log_stream_name log stream name

optional arguments:
-h, --help show this help message and exit
--aws-access-key-id AWS_ACCESS_KEY_ID
aws access key id
--aws-secret-access-key AWS_SECRET_ACCESS_KEY
aws secret access key
--aws-session-token AWS_SESSION_TOKEN
aws session token
--profile AWS_PROFILE
aws profile
--aws-region AWS_REGION
aws region
-f FILTER_PATTERN, --filter-pattern FILTER_PATTERN
A valid CloudWatch Logs filter pattern to use for
filtering the response. If not provided, all the
events are matched.
-w, --watch Query for new log lines constantly
-G, --no-group Do not display group name
-S, --no-stream Do not display stream name
--timestamp Add creation timestamp to the output
--ingestion-time Add ingestion time to the output
-s START, --start START
Start time (default 5m)
-e END, --end END End time
--no-color Do not color output
-q QUERY, --query QUERY
JMESPath query to use in filtering the response data

FILTER_PATTERNとQUERYとSTARTについて

  • FILTER_PATTERN: ログを絞り込めます。かなり自由なフィルタリングが可能です。

  • QUERY: ログの出力を整形できます。JMESPathをつかって結果をCSVにすることも可能です。

  • START: いつ以降のデータを取得するかを指定できます。CloudWatchのバグ(らしきissuesが....)によりコレを指定しないとエラーになります。

    • awslogsの振る舞いとしてデータが無いとエラーになるので、データがある期間を含むように指定する必要があります。

注意点

JMESPathはログがJson形式になっているとかなり扱いやすく、非常に便利です。
FILTER_PATTERNはJson形式のフィルタの場合、Json形式以外のログはそのままノンフィルターで出力しちゃいます。
他のシステムでログを再利用する場合、形式には一貫性が必要になります。

15
10
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
15
10