FintleWoodle
FintleWoodle

Reputation: 157

AWS list instances that are pending and running with one CLI call

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!

Upvotes: 0

Views: 442

Answers (2)

Fer Trevino
Fer Trevino

Reputation: 31

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

Upvotes: 1

fedonev
fedonev

Reputation: 25639

Shorthand syntax with commas:

Values=running,pending

Upvotes: 2

Related Questions