0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CLI --queryオプションでの出力形式の違いについて

Last updated at Posted at 2018-12-03

以下をおさらいする:[How to Filter the Output with the --query Option - Controlling Command Output from the AWS Command Line Interface - AWS Command Line Interface]
(https://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html#controlling-output-filter)

おさらい

フィルタ前のオリジナル出力は以下であるとする(上記ドキュメントより抜粋)。

$ aws ec2 describe-volumes --query 'Volumes[0]'
{
    "AvailabilityZone": "us-west-2a",
    "Attachments": [
        {
            "AttachTime": "2013-09-17T00:55:03.000Z",
            "InstanceId": "i-a071c394",
            "VolumeId": "vol-e11a5288",
            "State": "attached",
            "DeleteOnTermination": true,
            "Device": "/dev/sda1"
        }
    ],
    "VolumeType": "standard",
    "VolumeId": "vol-e11a5288",
    "State": "in-use",
    "SnapshotId": "snap-f23ec1c8",
    "CreateTime": "2013-09-17T00:55:03.000Z",
    "Size": 30
}

ドキュメントより、これをフィルタする例の一つ目。これを見て、AvailabilityZoneという変数名がAZになるのはなぜか、そうしなければいけないものなのかという疑問が湧いた。

$ aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}'
[
    {
        "InstanceId": "i-a071c394",
        "AZ": "us-west-2a",
        "ID": "vol-e11a5288",
        "Size": 30
    },
    {
        "InstanceId": "i-4b41a37c",
        "AZ": "us-west-2a",
        "ID": "vol-2e410a47",
        "Size": 8
    }
]

この疑問を解消するのにドキュメントの次の例を見ると良かった。

$ aws ec2 describe-volumes --query 'Volumes[*].[VolumeId, Attachments[0].InstanceId, AvailabilityZone, Size]'
[
    [
        "vol-e11a5288",
        "i-a071c394",
        "us-west-2a",
        30
    ],
    [
        "vol-2e410a47",
        "i-4b41a37c",
        "us-west-2a",
        8
    ]
]

こちらの例は出力jsonに変数名を含まない。これは--queryに指定したオプションの違いによる。

1つ目の例では出力jsonをディクショナリオブジェクトとして指定している(queryの中の{ID:VolumeId,...,Size:Size}の中カッコ)。

2つ目の例では出力jsonをリストオブジェクトとして指定している(queryの中の[VolumeId,...,Size]の大カッコ)。

ディクショナリは値だけでなく変数名が必要。よって変数名を指定するのに、{出力jsonに割り付ける変数名:入力jsonの変数名}と指定する。リストは変数名が不要であるため、単に[入力jsonの変数名]のみ指定する(割り付ける変数名は指定できない)。

これを踏まえた別の例は次のとおり。

リスト形式で出力(しかし何がtrue/falseか分からない)

$ aws ec2 describe-images --image-ids ami-06cd52961ce9f0d85 --query 'Images[*].[Description,BlockDeviceMappings[0].Ebs.Encrypted]'
[
    [
        "Amazon Linux AMI 2018.03.0.20180811 x86_64 HVM GP2",
        false
    ]
]

ディクショナリ形式で出力(元の変数名を生かす)

$ aws ec2 describe-images --image-ids ami-06cd52961ce9f0d85 --query 'Images[*].{Description:Description,Encrypted:BlockDeviceMappings[0].Ebs.Encrypted}'
[
    {
        "Encrypted": false,
        "Description": "Amazon Linux AMI 2018.03.0.20180811 x86_64 HVM GP2"
    }
]
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?