-
Notifications
You must be signed in to change notification settings - Fork 4k
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
aws ec2 describe-instances --filters tag: seems to be not working (or docs is missing/misleading) #368
Comments
To filter on all instances that have a tag with a name or key of
|
Thanks, that works. It would be great if the example was in documentation (think it's not obvious, it is slightly different from other filters). Also the documentation for tag-key says: "If you want to list only resources where Purpose is X, see the key filter later in this table." As far as I can tell it should say "see the tag: filter". Documentation in the above refers to both "aws ec2 describe-instances help" and http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html which seem to be the same (i.e. generated from the same source). |
I agree that an example would be very helpful. I will tag this as a documentation issue. Thanks. |
Took a while to find this considering how useful, yes, would be nice to have one example in aws ec2 describe-instances doc (http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html) |
Yes, this now works. The example supplied by AWS Docs is a bit confusing but to clarify. I have a tag called 'Resource' and each instance is assigned a value either 'test', 'stage' or 'live'. To find all instances (running or stopped) with the resource tag of 'test' this is the syntax: aws ec2 describe-instances --filters Name=tag:resource,Values=test --profile test-aws |
Im having the same problem with --query. I just want the Name of the instance in the report and its coming out black. I'm using the following query describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,Platform,Tags.Value,State.Code,Tags.Values]' --output table |
Are you using the latest version of was cli ? It works for me
-- Seb On 27 Feb 2014, at 15:16, derrickfn notifications@github.com wrote:
|
Greetings Sebsto, The command is working, but it is not returning the actual tag values. Its giving me None(NULL), just like it did you. For the Name element we plug in names for all of your servers to make them easier to identify. We also Tag the environment they they are part of. Thanks in advance for the help! |
Sorry - I answered too fast For Platform : this is expected as only Windows instances have a Platform attribute in the JSON description OK: Not OK: -- Seb On 27 Feb 2014, at 15:38, derrickfn notifications@github.com wrote:
|
@sebasto The issue with the above query is that However, in the latest CLI version (1.3.0) just released, there's a more direct way to filter by specific names. For example, if you wanted the name of all your instances in a list you can now say:
|
@jamesls How would one go about expanding this query to include more results in the output? For example I am using the following to list things like InstanceID, AZ, etc |
Have you tried:
|
Sorry, that's not it. How about this?
|
Yes, that works but only gives me the name tag. I'm trying to get that and other information like InstanceID, AZ, etc. |
Well, this works for me:
You could expand this to get other items besides just InstanceId. |
Sorry to bump like this, but I have an issue with a query: aws ec2 describe-instances --query Reservations[].Instances[].SecurityGroups[?GroupName==default].GroupId I use the above to try to extract the groupid of a certain group (name=default in this case). But nothing is returned. Any ideas? |
I think I am stuck somewhere, when i run this I get None everywhere aws --region us-east-1 ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key== |
This is because .Value[] just returns the value of your name tags. All the other attributes requested after that does not exist in this output |
The filters have a awful syntax. I can google with you, try to figure out. On 12/03/2014 06:14 PM, asvarshn wrote:
|
I have got this working for me.. aws --region eu-west-1 ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key== |
Alright, this wasn't obvious, from the above, so hopefully it'll help somebody. This lists in a nice table format some useful fields including the Name tag (if you use that).
|
|
The help is still super confusing about the tag filter. It says:
which seems to suggest that you can do:
which of course is not correct. |
If anyone has a hard time figuring it out how to sort tags, following command works for me: |
If you need to include tags with a : in them you need to quote, i.e. |
I too was stymied by the impenetrable and misleading documentation on this. |
I was trying to figure out how to get the IP address of an instance based on a tag (in this case, "Name"). I tried a few different things with --query and --filters, but it seems that getting from one nested parameter to another is complicated, or maybe not possible just using one or the other approach. Combining --query and --filters did the trick:
|
This comes up at the top when I search for Here's how you would achieve this: import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.filter(Filters=[{'Name': 'tag:Name', 'Values': ['api-*']}]):
print instance |
@adeelx for Python sure but the issue is with the bash awscli usage |
Below command works for me
|
I wanted to filter my instances to those that had a specific prefix in the Name tag, and then ensure that in the output table the full Name was listed, the only way to achieve that seems to be to use a
|
I had similar issues and it was quite a struggle to get to this: |
5 years later, and that same terrible example is still in the documentation, and a better example still doesn't exist. |
My Google search for how to use filters with the describe_* API calls has led me here, and I can't help but shake my head after reading @tolidano 's last comment. |
In case you need to filter by the tag key only, you can do something like
Or simply use the |
I'm looking to get ec2 platform type i.e. (Windows or Linux) using the lambda function filters. In case if anyone has done it could you please share the same |
'Windows' is returned by the "Platform" field for Windows instances. None is returned for Linux instances. The query at the top of this thread still works today.
|
if need exactly instance name |
Sharing what i have used:
|
aws ec2 describe-instances --filters Name=tag:<YOUR_TAG>,Values="<YOUR_VALUE>" --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,InstanceType,InstanceLifecycle,Tags[?Key==`<YOUR_TAG`]|[0].Value]' --output table |
I've used below filters to get the list of instances with public IP and their respective name usign --filter options and it works fine. aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" "Name=tag:OS,Values=AMAZON/LINUX2" --query "Reservations[].Instances[].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" --output table |
I have used below query to get AZ, InstanceType, Name, PrivateIpAddress, PublicIpAddress and state of the running ec2 instances: aws ec2 describe-instances --query 'Reservations[].Instances[].{State:State.Name,PublicIpAddress:PublicIpAddress,InstanceType:InstanceType,PrivateIP:PrivateIpAddress,AZ:Placement.AvailabilityZone,Name:Tags[?Key== To filter the details by name, i am using grep with -i option to have case-insensitive search instead of filters by name.. P.S. : While writing command, i was mostly struggling with the pipe operator highlighted before [0].Value in tag filter of name. (might be helpful for someone struggling with the command) |
@vaibhavg2510 while that approach works, and kudos, it does "waste" API commands. In a busy account, this makes you more subject to throttling, long running processes due to exponential backoff/retry, etc. |
Bump. The |
I spoke with the EC2 writers about this. They told me that they would work on an example to clarify this. In the interim, the syntax is documented in the EC2 User Guide here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI I hope this helps! |
I'm the latest victim I think that had to come here to get my answer. Are the plans to consolidate the docs still existing? Even a link from the API page to the above comments link (or maybe at this stage this repo) would be beneficial. |
I'm sorry, I've moved to a new role and no longer work in this space. I'll pass your comments on, however. |
Please also add documentation for how you can filter on absence of a value, for example:
What should you indicate in place of |
Trying to use aws ec2 describe-instance (version 1.1.0) with tag: filter which is mentioned in docs at http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html but it doesn't seem to be working.
Quote from documentation: "tag: --filters the response based on a specific tag/value combination."
Tried several formats (just guessing, trying format used by http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html) but no luck.
This does not complain but returns no instances:
aws ec2 describe-instances --filters Name=tag:,Values=Name=ADS-prod-ads
This one fails:
aws ec2 describe-instances --filters tag:Name=ADS-prod-ads
Unknown key 'tag:Name' for parameter --filters, valid choices are: Values, Name
Just for reference, using tag-value works (just to show that tag Name=ADS-prod-ads exists):
aws ec2 describe-instances --filters Name=tag-value,Values=ADS-prod-ads | jq '.Reservations[].Instances[].Tags[]'
{
"Key": "Name",
"Value": "ADS-prod-ads"
}
The text was updated successfully, but these errors were encountered: