4

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

instance-name,instance-id,volumes-associated

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.

drishti ahuja
  • 141
  • 1
  • 1
  • 3

4 Answers4

7

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.

John Rotenstein
  • 871
  • 7
  • 16
2

Another example, output is in json format:

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

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
0

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
Jenny D
  • 27,780
  • 21
  • 75
  • 114