bluethundr
bluethundr

Reputation: 1325

List EBS VolumeID and Instance ID in AWS Query

I need to list EBS VolumeID and the instance that it's attached to using the aws cli. This is the line I used:

 aws ec2 describe-volumes --output text --query 'Volumes[*].{VolumeID:VolumeId, Instance:InstanceId}' | head -5
None    vol-07210e47
None    vol-743d1234
None    vol-933d12d3
None    vol-493c1309
None    vol-1e3b145e

For some reason the instance IDs are showing as none. When the unfiltered output of the command shows that they're there:

 aws ec2 describe-volumes | head -25
{
    "Volumes": [
        {
            "AvailabilityZone": "us-east-1d", 
            "Attachments": [
                {
                    "AttachTime": "2013-09-05T15:17:39.000Z", 
                    "InstanceId": "i-c28e20ae", 
                    "VolumeId": "vol-07210e47", 
                    "State": "attached", 
                    "DeleteOnTermination": false, 
                    "Device": "/dev/sda1"
                }
            ], 

What am I doing wrong?

Upvotes: 2

Views: 2434

Answers (1)

kenlukas
kenlukas

Reputation: 3973

You're not querying into Attachments. This worked for me:

aws ec2 describe-volumes --output text --query 'Volumes[*].Attachments[].{VolumeID:VolumeId,InstanceID:InstanceId}'

This is a good link:

https://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html

Upvotes: 3

Related Questions