0

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*************   |
Zac Anger
  • 143
  • 7

1 Answers1

0

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