I wasn't able to figure out how to see the amount of running instances in CloudWatch. So I decided to measure them myself through the AWS PHP SDK which actually turned out to be much easier than I thought.
Here is the code from my cron command for anyone that wants to know:
public function fire(){
$client = AWS::get('CloudWatch');
$containers = [
'production',
'staging'
];
foreach($containers as $container){
$client->putMetricData([
'Namespace' => 'Company',
'MetricData' => [
[
'MetricName' => 'NumberOfInstances',
'Timestamp' => time(),
'Value' => $this->_getInstanceCount($container),
'Dimensions' => [
[
'Name' => 'Environment',
'Value' => AwsHelper::getEnvironment($container)
],
[
'Name' => 'Container',
'Value' => $container
]
]
]
]
]);
}
}
protected function _getInstanceCount($container){
$beanstalk = AWS::get('ElasticBeanstalk');
$resources = $beanstalk->describeEnvironmentResources([
'EnvironmentName' => AwsHelper::getEnvironment($container)
])->get('EnvironmentResources');
return count($resources['Instances']);
}
AwsHelper is a custom class that is only used here to retrieve the environment of a container. AWS is the official AWS SDK for PHP: http://aws.amazon.com/sdkforphp/
I used these docs: