I have a custom metrics, and I can get data from bash:

aws cloudwatch get-metric-statistics --namespace System/Detail/Linux \
--metric-name LoadAverage1Min --start-time 2017-01-04T00:00:00 \
--end-time 2017-01-04T02:00:00 --period 60 --statistics Average \
--dimensions Name=InstanceId,Value=i-03d55dba88912f054
    "Datapoints": [
            "Timestamp": "2017-01-04T00:33:00Z",
            "Average": 0.0,
            "Unit": "Count"
            "Timestamp": "2017-01-04T01:44:00Z",
            "Average": 0.0,
... etc...

But it does not work for lambda. The question is: how to get data for custom metrics?

I'm trying to do the sane using lambda and boto3 (sorry, beginner in Python):

import boto3
import logging
from datetime import datetime
from datetime import timedelta

#setup simple logging for INFO
logger = logging.getLogger()

#define the connection
ec2 = boto3.resource('ec2')
cw = boto3.client('cloudwatch')

def lambda_handler(event, context):
    # Use the filter() method of the instances collection to retrieve
    # all running EC2 instances.
    filters = [{
            'Name': 'instance-state-name', 
            'Values': ['running']

    #filter the instances
    instances = ec2.instances.filter(Filters=filters)

    #locate all running instances
    RunningInstances = [instance.id for instance in instances]

    dnow = datetime.now()

    for instance in instances:
        inst_name = [tag['Value'] for tag in instance.tags if tag['Key'] == 'Name'][0]
        if inst_name != 'instances-name-i-need':

        response = cw.get_metric_statistics(
                    'Name': 'InstanceId',
                    'Value': 'instance.id'
        print response

But when I run the function via lambda test I receive empty responses only:

{u'Datapoints': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '98ee...6ba', 'HTTPHeaders': {'x-amzn-requestid': '98ee...6ba', 'date': 'Thu, 05 Jan 2017 22:52:12 GMT', 'content-length': '338', 'content-type': 'text/xml'}}, u'Label': 'LoadAverage1Min'}
{u'Datapoints': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '98f4...a01', 'HTTPHeaders': {'x-amzn-requestid': '98f4...a01', 'date': 'Thu, 05 Jan 2017 22:52:13 GMT', 'content-length': '338', 'content-type': 'text/xml'}}, u'Label': 'LoadAverage1Min'}
{u'Datapoints': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '98f8...764', 'HTTPHeaders': {'x-amzn-requestid': '98f8...764', 'date': 'Thu, 05 Jan 2017 22:52:13 GMT', 'content-length': '338', 'content-type': 'text/xml'}}, u'Label': 'LoadAverage1Min'}

However I was able to get data from AWS namespace with

response = cw.get_metric_statistics(
    StartTime=datetime.utcnow() - timedelta(days=2) ,
    EndTime=datetime.utcnow(), Period=86400,
    Statistics=['Average'], Unit='Bytes',
        {'Name': 'BucketName', 'Value': 'bucket-name'},
        {u'Name': 'StorageType', u'Value': 'StandardStorage'}

The data for the metrics does exist: enter image description here

so, how can I get it?


Very stupid error:

'Value': 'instance.id'

should be

'Value': instance.id

because instance.id is a variable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.