1

Is there a way to display a Cloudwatch metric showing the number of running EC2 instances built from a given AMI at a given time. I'm trying to track the size of a system that scales up and down over the course of the day, and I'd like to add a graph to a Cloudwatch dashboard if at all possible. However, when I open the metrics section of Cloudwatch all I see are the same instance-level stats I'd get looking at a specific EC2 instance, and all my Google searches ultimately take me back to instance-level metrics.

Is there a simple metric in Cloudwatch that counts the number of running EC2 instances for an AMI, and if so, how do I add start tracking that in Cloudwatch?

Eric Hydrick
  • 113
  • 7

2 Answers2

1

You can create custom CloudWatch metrics to track this. Create a CloudWatch event that triggers when an instance goes into a Running, Stopped, or Terminated state, of which then invokes a Lambda function. Use that Lambda function to publish to the custom CloudWatch metric. The only issue with this method is that Lambda does not save state, so you would need to either do a describe api call on the metric to get its last data point and then +/- 1 depending on the event, or do a describe instances api call and filter the ami and instances in a running state, and publish the count. Note that I believe CloudWatch only sends to Lambda the instance-id and the state, so you would have to run describe instances on the instance-id to confirm it is running the ami you wish to track.

I ran something similar in the past to track in real-time running instances against service limits.

byronthree
  • 36
  • 2
  • Not something I really want to have to do, but this appears to really be the only option for it, so I'll accept it. – Eric Hydrick Apr 26 '18 at 19:18
0

To track an event in CloudWatch, you create an emergency signal that controls the metric with a given threshold. When creating an alarm, you can add an "action" to send a notification. AWS processes notifications via SNS (Simple Notification Service). You can subscribe to the notification subject, and then you will receive an email message for your alarm.