List all volumes attached to list of instances using aws cli
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 i-0d9c9b94b6583af4c Database vol-629feaa2 i-3da61da2 Web B vol-a6d443e7 i-7d264642 Web A vol-7840ce4a
There might be multiple EBS volumes associated with an instance.
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