30

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?

1
  • Run the Windows commands from a powershell prompt in Windows. Then it will return results. Commented Jun 2, 2017 at 20:04

3 Answers 3

43
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

7
  • 1
    Adding a brief description of what the command is doing might be helpful.
    – dawud
    Commented Mar 27, 2015 at 20:45
  • would this only work in non-windows based CLI ? I tried it in Windows CMD - it didn't but - in CentOS - it worked. Commented Jun 28, 2015 at 23:36
  • Where is some documentation on the query syntax???
    – cosbor11
    Commented Feb 9, 2016 at 20:23
  • Yes. Here you are: docs.aws.amazon.com/cli/latest/userguide/… Commented Feb 10, 2016 at 21:01
  • The AWS docs only has a few examples. Isn't there a full spec on the syntax?
    – cosbor11
    Commented Feb 12, 2016 at 8:20
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.

1

For info, listing all instances names with their instance ID:

aws ec2 describe-instances \
  --filters 'Name=instance-state-name,Values=running' \
  --query 'Reservations[].Instances[].{Name:Tags[?Key==`Name`].Value | [0],InstanceId:InstanceId}' --output table

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .