LoginSignup
1
0

More than 1 year has passed since last update.

AWS CLIの--queryについて殴り書き

Last updated at Posted at 2022-05-28

この記事

AWS CLIのqueryオプションと色々格闘した時に知ったことのメモです

前知識

AWS CLIのqueryオプションはJMESPathを用いています
こちらのサイトで色々とお試しできます

対象

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara
  • describe-instancesコマンド
  • インスタンスは2台で、それぞれのNameタグは以下のとおりです
    • dohara-dev-xxx
    • dohara-dev-yyy
  • 見やすさのため、必要な項目以外は省略しています
{
  "Reservations": [
    {
      "Groups": [],
      "Instances": [
        {
          ...
          
          "InstanceId": "i-06c93dcc7a43c9b2f",
          
          ...
          
          "BlockDeviceMappings": [
            {
              "DeviceName": "/dev/xvda",
              "Ebs": {
                "AttachTime": "2022-03-29T02:10:09+00:00",
                "DeleteOnTermination": true,
                "Status": "attached",
                "VolumeId": "vol-06c873b06b2a76350"
              }
            }
          ],
          
          ...
          
          "Tags": [
            {
              "Key": "Name",
              "Value": "dohara-dev-xxx"
            },
            {
              "Key": "cloudpack",
              "Value": "Dohara"
            },
            {
              "Key": "Owner",
              "Value": "Dohara"
            }
          ],
          
          ...
          
          "CpuOptions": {
            "CoreCount": 1,
            "ThreadsPerCore": 1
          },
          
          ...
          
        }
      ],

      ...

    },
    {
      "Groups": [],
      "Instances": [
        {
          
          ...
          
          "InstanceId": "i-0e89607f349c14819",
          
          ...
          
          "BlockDeviceMappings": [
            {
              "DeviceName": "/dev/xvda",
              "Ebs": {
                "AttachTime": "2022-03-29T04:47:24+00:00",
                "DeleteOnTermination": true,
                "Status": "attached",
                "VolumeId": "vol-0e0e4e18692e57ba8"
              }
            }
          ],
          
          ...
          
          "Tags": [
            {
              "Key": "cloudpack",
              "Value": "Dohara"
            },
            {
              "Key": "Owner",
              "Value": "Dohara"
            },
            {
              "Key": "Name",
              "Value": "dohara-dev-yyy"
            }
          ],
          
          ...
          
          "CpuOptions": {
            "CoreCount": 1,
            "ThreadsPerCore": 1
          },
          
          ...
          
        }
      ],

      ...

    }
  ]
}

リストとネスト

リスト

{
  "xxx": [
    {
      "yyy": "hoge"
    },
    {
    
      ...
    
    },
    
    ...
    
  ]
}

[]で囲われた各要素はリストとなります

xxx[<Index>].yyyとすることで取得可能です

  • xxx[]xxx[*]で少し出力が異なります
    • xxx[]での出力をフラット化するというらしいです(参考

xxx[]パターン

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].Tags[]"
[
  {
    "Key": "Name",
    "Value": "dohara-dev-xxx"
  },
  {
    "Key": "cloudpack",
    "Value": "Dohara"
  },
  {
    "Key": "Owner",
    "Value": "Dohara"
  },
  {
    "Key": "cloudpack",
    "Value": "Dohara"
  },
  {
    "Key": "Owner",
    "Value": "Dohara"
  },
  {
    "Key": "Name",
    "Value": "dohara-dev-yyy"
  }
]

xxx[*]パターン

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[*].Tags[*]"
[
  [
    [
      {
        "Key": "Name",
        "Value": "dohara-dev-xxx"
      },
      {
        "Key": "cloudpack",
        "Value": "Dohara"
      },
      {
        "Key": "Owner",
        "Value": "Dohara"
      }
    ]
  ],
  [
    [
      {
        "Key": "cloudpack",
        "Value": "Dohara"
      },
      {
        "Key": "Owner",
        "Value": "Dohara"
      },
      {
        "Key": "Name",
        "Value": "dohara-dev-yyy"
      }
    ]
  ]
]

ネスト

{
  "xxx": {
    "yyy": {
      "zzz": "hoge"
    }
  }
}

{}で囲っていくことで要素がネストされていきます

xxx.yyy.zzzとすることで取得可能です

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId"
[
  "vol-06c873b06b2a76350",
  "vol-0e0e4e18692e57ba8"
]

ラベル

クエリ結果は基本的にValueのみが出力され、Keyは出力されません
※ Keyも出力する方法があったら、教えていただけたらありがたいです
そこで、それぞれのクエリ結果に対してラベルを付与できます

基本形

{
  "xxx": {
    "yyy": "hoge"
  }
}

xxx.yyyの場合の出力

"hoge"

xxx.{yyy: yyy}の場合の出力

{
  "yyy": "hoge"
}

クエリ結果「VolumeId」にラベル「hogehoge」を付与して出力

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].BlockDeviceMappings[].Ebs.{hogehoge: VolumeId}"
[
  {
    "hogehoge": "vol-06c873b06b2a76350"
  },
  {
    "hogehoge": "vol-0e0e4e18692e57ba8"
  }
]

比較

特定のValueに対して比較演算子を用いることで、リストにおいて対象の絞り込みが可能です
例えば、全タグのうちNameタグのみを出力するなど

基本形

{
  "xxx": [
    {
      "yyy": "a1",
      "zzz": "a2"
    },
    {
      "yyy": "b1",
      "zzz": "b2"
    }
  ]
}

xxx[?yyy=='a1']で絞り込み可能

[
  {
    "yyy": "a1",
    "zzz": "a2"
  }
]
  • 文字列(ここでは「a1」)は'または`で囲む必要があります

タグのキーが「Name」の場合のみ、タグを出力

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].Tags[?Key=='Name']"
[
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-xxx"
    }
  ],
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-yyy"
    }
  ]
]

補足

複数条件を組み合わせる場合

AND : xxx[?(yyy=='a1' && zzz=='a2')]
OR : xxx[?(yyy=='a1' || yyy=='b1')]

タグのキーが「Name」または「Owner」の場合のみ、タグを出力

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].Tags[?(Key=='Name' || Key=='Owner')]"
[
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-xxx"
    },
    {
      "Key": "Owner",
      "Value": "Dohara"
    }
  ],
  [
    {
      "Key": "Owner",
      "Value": "Dohara"
    },
    {
      "Key": "Name",
      "Value": "dohara-dev-yyy"
    }
  ]
]

関数

JMESPathでは様々な関数が用意されています

contains

特定の文字列を含むかを判定します

タグの値に「xxx」が含まれている場合のみ、タグを出力

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].Tags[?contains(Value, 'xxx')]"
[
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-xxx"
    }
  ],
  []
]

sort_by

指定した要素で出力結果をソートします

タグをキーでソートして出力

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].sort_by(Tags, &Key)"
[
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-xxx"
    },
    {
      "Key": "Owner",
      "Value": "Dohara"
    },
    {
      "Key": "cloudpack",
      "Value": "Dohara"
    }
  ],
  [
    {
      "Key": "Name",
      "Value": "dohara-dev-yyy"
    },
    {
      "Key": "Owner",
      "Value": "Dohara"
    },
    {
      "Key": "cloudpack",
      "Value": "Dohara"
    }
  ]
]
  • sort_by未使用時はリストの例を参照してください
  • reverse(sort_by())とすることで逆転させることが可能です

その他

データ型

各値にはstringやnuberなどの型が定義されており、そこを意識しないと時折沼にハマります
type()を用いることで確認可能です

aws ec2 describe-instances --filters Name=tag:Owner,Values=Dohara --query "Reservations[].Instances[].{InstanceId: InstanceId, type_InstanceId: type(InstanceId), CoreCount: CpuOptions.CoreCount, type_CoreCount: type(CpuOptions.CoreCount)}"
[
  {
    "InstanceId": "i-06c93dcc7a43c9b2f",
    "type_InstanceId": "string",
    "CoreCount": 1,
    "type_CoreCount": "number"
  },
  {
    "InstanceId": "i-0e89607f349c14819",
    "type_InstanceId": "string",
    "CoreCount": 1,
    "type_CoreCount": "number"
  }
]

また、to_string()などの型変換用の関数も存在します

参考

1
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
1
0