Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (12.9k points)

Using aws-cli 1.3.6 I am trying to get a simple table of my ec2 instances with the Name and state. I have been looking at the --query and JMESpath documentation and I have been able to select the "Value" item of a Map which "Key" item is equal to Name. This is useful to get the instance-name. Therefore, the code below seems to work

aws ec2 describe-instances --output table --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value'

And delivers this:

--------------------------

|DescribeInstances|

+-----------------------+

|  Name1                |

|  Name2                |

+-----------------------+

However, if I want to add the state, things get not as I would have expected. Using

aws ec2 describe-instances --output table --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value,State.Name]'

Delivers

--------------------------

|DescribeInstances|

+-----------------------+

|  Name1                |

|  stopped               |  

|  Name2                |

|  stopped               |

+-----------------------+

instead of a two column table with name and state.

If we turn the output to JSON, we can see that the Tags selection returns a list (one-element list) and that's probably the issue:

[

    [

        [

            "Name1"

        ],

        "stopped"

    ],

    [

        [

            "Name2"

        ],

        "stopped"

    ]

]

I have not been able to turn this list into an scalar by selecting the first element. This, does not work. Returns an empty list as the Name.

aws ec2 describe-instances --output json --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[0],State.Name]'

The same as this

aws ec2 describe-instances --output json --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[][0],State.Name]'

The only way I have figured out of addressing this is by means of the join function. Since I only expect one element, it is ok but I seems to be a little bit hacky.

aws ec2 describe-instances --output table --query 'Reservations[].Instances[].[join(`,`,Tags[?Key==`Name`].Value),State.Name]'

--------------------------------

|    DescribeInstances    |

+-------------+---------------+

|  Name1      |  stopped  |

|  Name2      |  stopped  |

+-------------+---------------+

The question, therefore, is: is there any way of picking the first element of the result of the filter (?Key==XXXX) bearing in mind that suffixing it with [0] seems not to work?

Thanks in advance!

1 Answer

0 votes
by (18.2k points)

The most convenient solution for your problem might be Pipe Expressions which has been made available in AWS CLI version 1.3.7.

A pipe expression simply combines two expressions that are separated by '|' character.

So if you want to pick the first element of the result of the filter, you can use the following command:

aws ec2 describe-instances --output table \

  --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, State.Name]'

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

+1 vote
1 answer
asked Sep 28, 2019 in AWS by chandra (29.3k points)
0 votes
1 answer
asked Apr 1, 2020 in AWS by chandra (29.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...