'AWS CLI to get only Volumeid and tagvalue

From AWS using CLI to get volume details via describe-volumes

Sample volume Details

VolumeId = Vol-********
Attachments.InstanceId = i-*********
Tags : Key = Name     Value = sometext

Expected Output via AWS CLI:

Vol-********   i-*********   sometext

Codes I tried:

aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,Tag:Tags}" --output text

Output what I am getting:

Vol-********   i-*********
TAG    Name    sometext

Looking for help to get output as below.

Expected Output via AWS CLI:

Vol-********   i-*********   sometext


Solution 1:[1]

If you are looking for a specific tag value of a volume:

aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[0].Value}" --output text

vol-*******   i-******     second

If you are looking for all the tags value, it will come as below: format= text

aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[].Value}" --output text

vol-0fa1417c2369e7440   i-0533e831213cf0cc4
TAG     second
TAG     test

If you are looking for all the tags value, it will come as below. format= json

aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,tag:Tags[].Value}" --output json

[
    {
        "InstanceId": "i-******",
        "tag": [
            "second",
            "test"
        ],
        "ID": "vol-******"
    }
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Rotenstein