I am using aws-cli and I need the list of all instance and the volumes associated with them.


describe-instances and describe-volumes are different way to list instances and volumes. But I need a consolidated list as in the above format. There could be multiple volumes associated with one instance.


This command will output:

  • The value associated with the 'Name' tag
  • Instance ID
  • EBS Volume ID

    aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value,InstanceId,BlockDeviceMappings[*].Ebs.VolumeId]' --output text
    Web B
    Web A

There might be multiple EBS volumes associated with an instance.


Another example, output is in json format:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].{Name:ImageId,InstanceId:InstanceId,VolumeInfo:BlockDeviceMappings}' --output json

In my case i needed to list all stopped instances and associated volumes for a clean up of cloud resources and cost savings. The following code will do the job for you. If you set it to json output is human friendly.

aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value,InstanceId,BlockDeviceMappings[*].Ebs.VolumeId]' --output text

Using this command I can be able to list the volume details with instance id and volume size.

aws ec2 describe-volumes –-query "Volumes[*].[Attachments[0].VolumeId,AvailabilityZone,Attachments[0].InstanceId,Attachments[0].State,Size]" --output text > test.txt

But my query I have to consolidate the whole instance list its includes instance id, instance state, region, platform, key pair name along with attached volumes name, volume id, volume size and mount path name like /dev/sda1,

Please help me with this.

Using this command can be able to get the instance details but I need to single query to get all the details.

aws ec2 describe-instances --filters "Name=instance-state-name,Values=*" --query "Reservations[].Instances[].[Tags[?Key==`Name`]| [0].Value,InstanceId,State.Name,InstanceType,Placement.AvailabilityZone,PrivateIpAddress,VolumeInfo:BlockDeviceMappings" --output text > instances.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.