4

Given the output of aws ec2 describe instances I am trying to display objects that have no Platform value and DO have a VpcId value. So far I have come up with this:

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[?Platform==`null` && ?VpcId!=`null`]'

But it errors out with:

Bad value for --query Reservations[*].Instances[?Platform==`null` && ?VpcId!=`null`]: Bad jmespath expression: Unknown token ?:
Reservations[*].Instances[?Platform==`null` && ?VpcId!=`null`]

I have been looking through http://jmespath.org/ and do not see how to execute multiple filter expressions.

Both

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[?Platform==`null`]'

and

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[?VpcId!=`null`]'

work, but I am trying to get both of those query's at once.

Roderick Day
  • 51
  • 1
  • 5
  • I would not rely on AWS CLI/API filters for any task but simple query. It seems it always a good idea to go with some SDK and high level language then keep playing with shell command trying to find workarounds and rely on blackbox functionality like filters implementation. – user2153517 Oct 30 '17 at 13:52
  • @user2153517 While that may well work for you, it's not really contributing to solving the actual problem (which is solved below and has an alternate as well). Many Ops types don't know/aren't interested in high level languages and could care less if it can be done better in one. Amazon officially supports and provides the command-line tools, so it's worthwhile to understand how they work for those that prefer them. – Roderick Day Oct 31 '17 at 16:19

2 Answers2

1

I was able to solve my logical problem (filter describe-instances for instances that are in a VPC but that aren't windows) with the help of this question.

My resulting query (wherein I'm looking for PrivateIPAddress) was:

aws --output json ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" |\
jq '.Reservations[].Instances | \
map(select(.VpcId != null)) |  map(select(.Platform != "windows") | .PrivateIpAddress)'

However the actual question I asked about combining filters remains unanswered, SO I will leave this open in the hopes of attracting an answer to that question.

kenorb
  • 5,943
  • 1
  • 44
  • 53
Roderick Day
  • 51
  • 1
  • 5
1

I took your command and removed 1 character - the 2nd ? mark. I added the option to format the output as text. I also realize that the awscli may have changed to support multiple queries since your original posting, so you will want to ensure that you're up to the latest version. This query works for me:

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[?Platform==`null` && VpcId!=`null`]' --output text
kenorb
  • 5,943
  • 1
  • 44
  • 53
LHWizard
  • 546
  • 4
  • 11