Listing instance name among other data with aws-cli 1.3.6

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?

The way you phrased this question hints towards the solution in fact, namely Pipe Expressions (only available as of version 1.3.7 of the aws-cli though, hence impossible to figure out at question time):

pipe-expression  = expression "|" expression

A pipe expression combines two expressions, separated by the | character. It is similar to a sub-expression with two important distinctions:

  1. Any expression can be used on the right hand side. A sub-expression restricts the type of expression that can be used on the right hand side.
  2. A pipe-expression stops projections on the left hand side for propagating to the right hand side. If the left expression creates a projection, it does not apply to the right hand side.

The emphasized part is key, as shown in the subsequent examples, notably:

If you instead wanted only the first sub list, ["first1", "second1"], you can use a pipe-expression:

foo[*].bar[0] -> ["first1", "first2"]
foo[*].bar | [0] -> ["first1", "second1"]

Solution

Thus, applying a pipe expression yields the desired result:

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

----------------------------------
|        DescribeInstances       |
+--------------------+-----------+
|  Name1             |  stopped  |
|  Name2             |  stopped  |
+--------------------+-----------+

It tells the Instance ID along with the server state

Command:
aws ec2 describe-instances --filter Name=tag:Name,Values=eep --query 'Reservations[*].Instances[*].{id:State,ID:InstanceId}' --output table

Query part in the above command
It changes as per the requirement
--query 'Reservations[*].Instances[*].{id:State,ID:InstanceId}' --output table

#!/bin/bash
for r in `aws ec2 describe-regions --query Regions[*].RegionName --output text`
do

    #echo $r
    aws ec2 describe-instances --region $r --query 'Reservations[*].Instances[*].{ID:InstanceId, type:InstanceType, launched:LaunchTime, name:Tags[?Key==`Name`].Value[]}' --output json | jq --arg R $r -r '.[] | .[] | [$R, .ID, .type, .launched, .name[0]] | @csv'

done

Output:

"us-east-1","i-054f8253b9ed0746d","t2.micro","2018-10-31T01:57:52.000Z","xxx"
"us-east-1","i-0638792b8b3057ce2","t2.nano","2018-10-23T03:49:24.000Z","yyy"