Home > Software design >  AWS list instances that are pending and running with one CLI call
AWS list instances that are pending and running with one CLI call

Time:06-18

I have an application which needs to know how many EC2 instances I have in both the pending and running states. I could run the two following commands and sum them to get the result, but that means my awscli will be making two requests, which is both slow and probably bad practice.

aws ec2 describe-instances \
    --filters Name=instance-state-name,Values="running" \
    --query 'Reservations[*].Instances[*].[InstanceId]' \
    --output text \
    | wc -l


aws ec2 describe-instances \
    --filters Name=instance-state-name,Values="pending" \
    --query 'Reservations[*].Instances[*].[InstanceId]' \
    --output text \
    | wc -l

Is there a way of combining these two queries into a single one, or another way of getting the total pending running instances using a single query?

Cheers!

CodePudding user response:

Shorthand syntax with commas:

Values=running,pending

CodePudding user response:

You can add several filter values as follows:

aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running,pending" \
--query 'Reservations[*].Instances[*].[InstanceId]' \
--output text \
| wc -l
  • Related