Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using --query to select tag by value not tab delimitated #758

Closed
catofclysm opened this issue Apr 22, 2014 · 6 comments
Closed

Using --query to select tag by value not tab delimitated #758

catofclysm opened this issue Apr 22, 2014 · 6 comments

Comments

@catofclysm
Copy link

Previously when I used --query to select a specific tag value by key it's text output was tab delimitated. After updating to the latest aws-cli (from 1.3.1 to 1.3.7) the tag value is now added after other values and on it's own line. For example:

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==Name].Value, State.Name]' --output text

The output of such is
InstanceID State
TagValue

Whereas before it would show
InstanceId TagValue State

@jamesls
Copy link
Member

jamesls commented Apr 22, 2014

Looking... I'm guessing it's related to the bug fix for #751 reported here: https://forums.aws.amazon.com/thread.jspa?messageID=534743#534743

@jamesls
Copy link
Member

jamesls commented Apr 22, 2014

When I installed v1.3.1 and tried the query above I get the same output as shown in the bug linked in #751. The second value is a list value instead of just the value of the tag:

$ aws --version
aws-cli/1.3.1 Python/2.7.5 Darwin/12.5.0
$ aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value, State.Name]' --output text

i-123   ['Name1']   running
i-123   ['Name2']   running
i-123   ['Name3']   stopped
i-123   ['Name4']   stopped

Is that the same output you were previously seeing?

The reason this is now happening with the latest release is that list values are now written out on their own separate line. The JSON values from the above query looks like:

[
    [
        "i-123",
        [
            "Name1"
        ],
        "running"
    ],
    [
        "i-123",
        [
            "Name2"
        ],
        "running"
    ]
]

In order to get them all in a single row, they need to be in a single list like:

[
    [
        "i-123",
        "Name1",
        "running"
    ],
    [
        "i-123",
        "Name2",
        "running"
    ]
]

So specifically, you'll need to say you want the first element in the list of tag values. This is possible in the develop branch of the CLI, but is not in an actual release just yet. The syntax for this is:

--query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value | [0], State.Name]'

I'll update this issue when a CLI release is pushed that has support for this syntax.

@jamesls
Copy link
Member

jamesls commented Apr 28, 2014

Closing, version 1.3.8 of the CLI now supports this syntax.

@jamesls jamesls closed this as completed Apr 28, 2014
uriagassi pushed a commit to uriagassi/sash that referenced this issue Jul 7, 2014
@Hount
Copy link

Hount commented Dec 15, 2015

Umgh, how come I see the exact same issue with awscli aws-cli/1.9.8 Python/2.7.10 Linux/4.2.6-201.fc22.x86_64+debug botocore/1.3.8?

aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name,InstanceId,InstanceType,Tags[0].Value]" --output table
| running| i-idididid | t2.micro | test-machinename |

aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name,InstanceId,InstanceType,Tags[?Key=='Name'].Value]" --output table

| running |
| i-idididid |
| t2.micro |
| test-machinename |

As a workaround
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,State.Name,Tags[?Key=='Name'].Value | [0], InstanceType]" --output table
| i-idididid| running | test-machinename | t2.micro |

@blueyed
Copy link
Contributor

blueyed commented May 4, 2016

@Hount
The key here is to use Tags[?Key=='Name'].Value | [0].

@rquadling
Copy link

rquadling commented May 24, 2019

Just as a way to demonstrate a use case.

alias ec2p="aws ec2 describe-instances --profile production --query \"sort_by(Reservations[].Instances[].{Name:Tags[?Key=='Name'].Value|[0],ID:InstanceId,Type:InstanceType,IP:PrivateIpAddress,State:State.Name,AMI:ImageId}, &Type) | []\" --output table"
alias ec2s="aws ec2 describe-instances --profile staging --query \"sort_by(Reservations[].Instances[].{Name:Tags[?Key=='Name'].Value|[0],ID:InstanceId,Type:InstanceType,IP:PrivateIpAddress,State:State.Name,AMI:ImageId}, &Type) | []\" --output table"

Looks like ..

------------------------------------------------------------------------------------------------------------
|                                                  DescribeInstances                                       |
+-----------------------+----------------------+--------------+-----------------+-----------+--------------+
|          AMI          |         ID           |     IP       |       Name      |   State   |    Type      |
+-----------------------+----------------------+--------------+-----------------+-----------+--------------+
|  ami-xxxxxxxx         |  i-05324595c01e9b008 |  10.0.21.54  |  Packer Builder |  stopped  |  c4.2xlarge  |
|  ami-xxxxxxxxxxxxxxxxx|  i-05a5ab56460a931fe |  10.0.19.18  |  prod-asg-app   |  running  |  m4.large    |
|  ami-xxxxxxxxxxxxxxxxx|  i-046989b3c9021fde4 |  10.0.72.14  |  prod-asg-app   |  running  |  m4.large    |
|  ami-xxxxxxxxxxxxxxxxx|  i-01939242d438d381b |  10.0.64.223 |  prod-asg-app   |  running  |  m4.large    |
|  ami-xxxxxxxx         |  i-0f6682340bde6a2c6 |  10.0.89.21  |  Bastion        |  running  |  t2.micro    |
+-----------------------+----------------------+--------------+-----------------+-----------+--------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants