Reputation: 469
How do I use AWS CLI to list all instances with name, state, instance size and AZ in the same line?
I got close with this:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[], Placement.AvailabilityZone,InstanceType,State.Name]' --output text
But that outputs the instance name below the rest. I want to keep them on the same line so I can copy to a spreadsheet.
Upvotes: 3
Views: 3365
Reputation: 641
If you don't wish to scratch your eyes out with the piping syntax, consider this simple shell workaround:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[], Placement.AvailabilityZone,InstanceType,State.Name]' | \
while read Zone Type State ; do
read Name
echo "$Name $Zone $Type $State"
done
Assuming that the original command returns 2 lines like these for each machine:
my-ec2-host-xyz
us-east-1d t2.micro running
The output of the above script will be:
my-ec2-host-xyz us-east-1d t2.micro running
This hack is easily understood and can readily be adapted to --output text of any complexity.
Upvotes: 1
Reputation: 36545
You need to change Tags[?Key==
Name].Value[]
to Tags[?Key==
Name].Value[] | [0]
; I think it's because Tags[?Key==
Name].Value[]
returns an array which the text output format doesn't know how to put on a single line, piping to [0]
extracts the (single) element out for you. So your full query should be :
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], Placement.AvailabilityZone,InstanceType,State.Name]' --output text
Upvotes: 6