ログってなんぼ

エンジニアのメモです

aws-cliの標準出力をjqで整形

AWSのEC2インスタンス一覧

$ aws ec2 describe-instances

{
    "Reservations": [
        {
            "OwnerId": "xxxxxxxxxxx", 
            "ReservationId": "r-xxxxxxx", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 

・
・
・

                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/xvda", 
                    "VirtualizationType": "hvm", 
                    "Tags": [
                        {
                            "Value": "example.com", 
                            "Key": "Name"
                        }
                    ], 
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}

jqを入れる

homebrewにあったのでそれを使う

$ brew install jq
==> Downloading https://homebrew.bintray.com/bottles/jq-1.4.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jq-1.4.yosemite.bottle.tar.gz

jqで色々

Tag

$ aws ec2 describe-instances | jq '.Reservations[].Instances[].Tags[]'

{
  "Value": "app.example.com",
  "Key": "Name"
}
{
  "Value": "db.example.com",
  "Key": "Name"
}
{
  "Value": "elasticsearch.example.com",
  "Key": "Name"
}
{
  "Value": "example.com",
  "Key": "Name"
}

Valueだけ表示

$ aws ec2 describe-instances | jq '.Reservations[].Instances[].Tags[].Value'

"app.example.com"
"db.example.com"
"elasticsearch.example.com"
"example.com"

JSONぽくないOUTPUT

プライベートIPアドレスとTag(Name)をタブ区切りで表示

$ aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | .PrivateIpAddress + "t" + .Tags[].Value'

192.168.1.125 app.example.com 192.168.1.14 db.example.com 192.168.1.250 elasticsearch.example.com 192.168.1.230 example.com

-r を指定して引用符を外し、書式設定の中にパイプを使って表示したい項目と区切り文字を指定しています

コンパクト表示

$ aws ec2 describe-instances | jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[]'

"GroupName": "web server", "GroupId": "sg-d5e80ow8" } { "GroupName": "WordPress powered by AMIMOTO -HVM--Version- 1-1-AutogenByAWSMP-1", "GroupId": "sg-7dc54320" } { "GroupName": "streamdata", "GroupId": "sg-d5es4rd3" } { "GroupName": "db", "GroupId": "sg-d5e8fd83y" }

通常表示だと見やすく綺麗に(色まで付けて)表示してくれますが

$ aws ec2 describe-instances | jq -c '.Reservations[].Instances[].NetworkInterfaces[].Groups[]'

{"GroupName":"web server","GroupId":"sg-d5e80ow8"} {"GroupName":"WordPress powered by AMIMOTO -HVM--Version- 1-1-AutogenByAWSMP-1","GroupId":"sg-7dc54320"} {"GroupName":"streamdata","GroupId":"sg-d5es4rd3"} {"GroupName":"db","GroupId":"sg-d5e8fd83y"}

-c オプションを指定することでコンパクトになります。

モノクロ表示

$ aws ec2 describe-instances | jq -M '.Reservations[].Instances[].NetworkInterfaces[].Groups[]'

"GroupName": "web server", "GroupId": "sg-d5e80ow8" } { "GroupName": "WordPress powered by AMIMOTO -HVM--Version- 1-1-AutogenByAWSMP-1", "GroupId": "sg-7dc54320" } { "GroupName": "streamdata", "GroupId": "sg-d5es4rd3" } { "GroupName": "db", "GroupId": "sg-d5e8fd83y" }

-M オプションでモノクロ表示になります。-C でカラーに(デフォルト)

select

select : 特定の文字や値を検索して絞り込み

$ aws ec2 describe-instances | jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[] | select(.GroupName == "web server")'

{ "GroupName": "web server", "GroupId": "sg-d5e80ow8" }

組み合わせて使ってみる

$ aws ec2 describe-instances | jq -r -c '.Reservations[].Instances[] | select(.NetworkInterfaces[].Groups[].GroupName == "web server") | .PrivateIpAddress + "t" + .Tags[].Value'

192.168.1.230 example.com

jq( ・∀・)イイ!!

aws-cliの標準出力をjqで整形 - Qiita に同様のポストをしています

※サンプルで掲載した出力中の各種IDなどの固有値は適当な文字列に置換してあります