Search code examples
linuxbashamazon-web-servicesamazon-ec2

How to list out all the EBS volumes in CLI


am using CLI to get the list of all EBS volumes with some specific tags.

When i use the specific tag am getting the output as none in my output... I need to list out all the instance which is Key:Environment Value: Prod I need the output in table format with the headings.....

I don't know why am getting the none output in the column Environment

As of now am using the query like:

aws ec2 describe-volumes --filter Name=tag:Environment,Values=prod --query 'Volumes[*].Attachments[].{VolumeID:VolumeId,InstanceID:InstanceId,State:State,Environment:Environment}'

Am getting the output like:

DescribeVolumes                              |
+-------------+-----------------------+-----------+-------------------------+
| Environment |      InstanceID       |   State   |        VolumeID         |
+-------------+-----------------------+-----------+-------------------------+
|  None       |  i-xxxxxxxxxxxxxxxxxx |  attached |  vol-xxxxxxxxxx  |

Please help me


Solution

  • When tinkering with parameters in the AWS CLI, I highly recommend reading:

    Here's a version of your command that extracts the specific tag:

    aws ec2 describe-volumes --filter Name=tag:Environment,Values=prod --query "Volumes[*].{VolumeID:Attachments[0].VolumeId,InstanceID:Attachments[0].InstanceId,State:Attachments[0].State,Environment:Tags[?Key=='Environment']|[0].Value}"
    

    It basically says "Include the Value of the tag that has a Key of Environment".

    You might need to play with the quote characters. This worked for me on a Mac, but Windows needs different quotes (eg single vs double).