19

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?

Chris Charles
  • 191
  • 1
  • 1
  • 4

2 Answers2

35
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

Peycho Dimitrov
  • 988
  • 7
  • 9
1

With Windows (cmd.exe, cmder.exe, etc.), one must use double quotes (") instead of single quotes (') around the query string. Otherwise the console simply prints the path in the query and doesn't even generate an error.

Jason
  • 11
  • 2