How do I get the most recent Cloudwatch metric data for an instance using Boto?

James picture James · May 5, 2013 · Viewed 18k times · Source

I'm trying to get the most recent data for CPU utilization for an instance (actually, several instances, but just one to start with), however the following call doesn't return any data:

cw = boto.cloudwatch.connect_to_region(Region)
cw.get_metric_statistics(
    300,
    datetime.datetime.now() - datetime.timedelta(seconds=600),
    datetime.datetime.now(),
    'CPUUtilization',
    'AWS/EC2',
    'Average',
    dimensions={'InstanceId':['i-11111111']}
    # for stats across multiple instances:
    # dimensions={'InstanceId':['i-11111111', 'i-22222222', 'i-33333333']}
)

Various posts on other sites indicate that checking the region is correct, checking that the period (first argument) is a multiple of 60, and (if you don't have detailed monitoring enabled) is greater than or equal to 300. I've checked all these things and I'm still not getting any data.

Answer

James picture James · May 5, 2013

This is a daylight savings time / time zone issue!

You need to use UTC time when receiving statistics from Cloudwatch:

    cw = boto.cloudwatch.connect_to_region(Region)
    cw.get_metric_statistics(
        300,
        datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
        datetime.datetime.utcnow(),
        'CPUUtilization',
        'AWS/EC2',
        'Average',
        dimensions={'InstanceId':['i-11111111']}
   )

From some experimentation it also seems that specifying multiple InstanceId dimensions will result in data only for the last specified instance (at least if detailed monitoring is not enabled).