How can I write a query for the total number of EC2 instances in an AWS account?
I know that I can do something like this to count unique instance IDs:
FROM ComputeSample SELECT uniqueCount(ec2InstanceId) WHERE provider=‘Ec2Instance’ and awsAccountId=‘446233199186’ AND … (whatever conditions identify the instances I want to count)
However, that’s subtly different from how many instances there are, and in a way that can become very misleading. It’s roughly equivalent if you only have long-lived instances, but if you have something like an active cluster that’s autoscaling frequently, or a worker pool where short-lived instances are being launched frequently, then the numbers you get from a query like that are highly dependent on the aggregation window size (aka “bucket size”, for one meaning of that term).
For example, if I have one new instance launching every 5 minutes which lasts 2-3 minutes and is then terminated, then if I do TIMESERIES 5 minutes I’ll get a line at 1, with an occasional jump to 2 if two instances happen to overlap into the same bucket. But if I do TIMESERIES 1 hour, the line will be at 12-13! I never have more than 1 instance active at a time, but that chart would show 12 pretty solidly.
So what I’m looking for is a way to count how many instances are currently there at any given point in time, and then I can use last() on that to graph it across any duration and aggregation window / bucket size I want, and get consistent and sensible results. Anyone know how to do that?