Search code examples
amazon-web-servicesaws-clijmespath

Reference Parent Element in JMESPath Filter Expression


I have the following JMESPath query

query="Reservations[].Instances[].{ \
    InstanceId: InstanceId, \
    RootDeviceVolumeId: BlockDeviceMappings[?DeviceName==\`/dev/sda1\`] \
       | [].Ebs.VolumeId | [0], \
    RootDeviceName: RootDeviceName \
}"

aws ec2 describe-instances --query $query

which gives output like this

+------------+------------------+----------------------+
| InstanceId | RootDeviceName   | RootDeviceVolumeId   |
+------------+------------------+----------------------+
|  i-12345678|  /dev/sda1       |  vol-abcdef12        |
|  i-98765432|  /dev/sda1       |  vol-ef123456        |
|  i-23456789|  /dev/sda1       |  vol-fedcba09        |
|  i-aabbccdd|  /dev/xvda       |  None                |
+------------+------------------+----------------------+

I'd like to find a way to reference the RootDeviceName from within the BlockDeviceMappings filter expression rather than hard-coding the /dev/sda1 device name, since sometimes it's /dev/xvda for instance. However, I can't find a way to reference a parent element in the filter expression.

Another option would be to map the RootDeviceName and InstanceId onto a projection of all devices and then pipe that to a filter expression, but the syntax doesn't seem to support including parent elements in projections either.

Am I missing something or is this simply a limitation of the JMESPath syntax?


Solution

  • We would need to have some mechanism that allows you to bind scope, which is something that is not currently possible... yet. I'm very interested in adding this. For now you'll have to use an alternate tool.