Score:0

AWS CLI : How to use "--query" to get output from nested JSON

ar flag

I am using aws-cli v2 to get the --query output from AWS Describe-volumes. In this, I am trying to get the volume id, size, Instance ID, state. Instance ID is nested in the AWS Describe-volumes.

Source :

{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2017-11-21T16:12:57+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-0e*******",
                    "State": "attached",
                    "VolumeId": "vol-0a******",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2017-11-21T16:12:57.122000+00:00",
            "Encrypted": false,
            "Size": 50,
            "SnapshotId": "snap-002********",
            "State": "in-use",
            "VolumeId": "vol-0a6d8675a63b65c20",
            "Iops": 150,
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        },

Currently I am able to get the Table output but in two small tables. Though I want the output in one single table.

aws ec2 describe-volumes --query "Volumes[*].{Size:Size,State:State,Type:VolumeType,Attachments[Instance:InstanceId],Volume:VolumeId}" --output table
|                  DescribeVolumes                  |
+------+---------+-------+--------------------------+
| Size |  State  | Type  |         Volume           |
+------+---------+-------+--------------------------+
|  10  |  in-use |  gp2  |  vol-0a468f3b9a1762acc   |
+------+---------+-------+--------------------------+
||                   InstanceID                    ||
|+---------------------------------------------+|
||  i-0d5a6a29093a7039f                            ||
|+---------------------------------------------+|

Is there any way to join the DescribeVolumes and InstanceID table in one single table. E.g.

---------------------------------------------
|                            DescribeVolumes                           |
+----------+-------+------------+-----------+--------------------------+
| Instance | Size  |   State    |   Type    |         Volume           |
+----------+-------+------------+-----------+--------------------------+
|  None    |  50   |  in-use    |  gp2      |  vol-0a6d*************   |
Score:0
pr flag

If you can assume there won't be more than one attachment, you could use:

aws ec2 describe-volumes --query "Volumes[*].{Size:Size,State:State,Type:VolumeType,Instance:Attachments[0].InstanceId,Volume:VolumeId}" --output table

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.