AWS EC2 Describe Instances – Filtering by Root device

amazon-web-servicesaws-clicommand-line-interface

Hoping you can help with this query. Hopefully the answer should allow me to construct better queries down the line.
I am trying to filter by the root device of a particular instance and get the root devices volume id. Could you explain how I could join two such queries. Searching for DeviceName /dev/sda1 and to then get the corresponding VolumeId.

aws ec2 describe-instances --instance-id i-02bc19e18ef09cdbe --query 'Reservations[].Instances[].BlockDeviceMappings[]'

[
    {
        "Ebs": {
            "AttachTime": "2017-04-20T13:32:04.000Z",
            "VolumeId": "vol-0118bd9c0a08164f2",
            "DeleteOnTermination": false,
            "Status": "attached"
        },
        "DeviceName": "/dev/sda1"
    }
]

Best Answer

This question is a little old, but here's a possible solution for you. Making one assumption - namely, that the root device is /dev/sda1. Which I think is a fairly good assumption for AWS. If you don't specify an instance-id, all of the instances will be returned. Then I'm doing a search of DeviceName equal to /dev/sda1. After the search term, a list is created with DeviceName and VolumeId.

aws ec2 describe-instances --query "Reservations[].Instances[].BlockDeviceMappings[?DeviceName == '/dev/sda1'].{DeviceName: DeviceName, VolumeID: Ebs.VolumeId}