Skip to content Skip to sidebar Skip to footer

How Do I Get The Most Recent Cloudwatch Metric Data For An Instance Using Boto?

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

Solution 1:

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).

Solution 2:

I was also seeing no data returned when setting units to "Megabytes", while setting units to "Bytes" returned data.

Both are allowed in the API reference.

data = conn.get_metric_statistics(period=60,start_time=start,end_time=end,metric_name="NetworkOut",namespace="AWS/EC2",statistics="Average",unit="Megabytes",dimensions={'InstanceId':'XXXXXX'})
print"data length: %d"%len(data)
    # data length: 0


data = conn.get_metric_statistics(period=60,start_time=start,end_time=end,metric_name="NetworkOut",namespace="AWS/EC2",statistics="Average",unit="Bytes",dimensions={'InstanceId':'XXXXXX'})
print"data length: %d"%len(data)
    # data length: 59

Solution 3:

I found that AWS/Billing metrics "live" only in one region - us-east-1.

Also, AWS CLI (aws cloudwatch get-metric-statistics) will errorr out if you try to grab more than 1440 data points from CloudWatch. If you encounter it set larger --period.

To avoid pitfalls you can use my EC2_Metrics_Plotter .

Post a Comment for "How Do I Get The Most Recent Cloudwatch Metric Data For An Instance Using Boto?"