AWS CLI Command Line: How to use “–query” to output multiple source lines

amazon ec2amazon-web-servicescommand-line-interfacequery

I am using aws-cli version 1.7.8 to get the --query output to create one record that is derived from multiple lines.
In this case I am trying to get specific information from describe-instances.

In the describe-instances command, we get lines / sections that refer to RESERVATIONS , INSTANCES , and TAGS .

I am able to simply run the new AWS CLI command to get the information from all three lines individually:

RESERVATION line:


aws ec2 describe-instances --instance-ids i-xxxxxxxx --query 'Reservations[*].ReservationId'

INSTANCE line:


aws ec2 describe-instances --instance-ids i-xxxxxxxx --query 'Reservations[*].[Instances[*].[InstanceId,ImageId]]'

TAG line:


aws ec2 describe-instances --instance-ids i-xxxxxxxx --query 'Reservations[].Instances[].[Tags[?Key==
Name]]'

I can run these 3 commands, and concatenate the results to form 1 record.

Does anyone know if there is a way I can run this as ONE (1) command, instead of 3 distinct commands?

I've tried PIPE and other forms of syntax, but I'm not able to find a good solution as of yet.

Can I get the equivalent output of these 3 commands from a single command?

Best Answer

aws ec2 describe-instances --instance-id i-77777777 \
  --query 'Reservations[*].Instances[*].[InstanceId,ImageId,Tags[*]]' \
  --output text

This command will print Instance Id, AMI ID, Key and Value Tags. You can see more examples here: Controlling Command Output from the AWS Command Line Interface