By using AWS re:Post, you agree to the Terms of Use

Boto3 SDK get_metric_data returned empty values

0

I have a lambda written in python 3.9 with boto3 cloudwatch client trying to retrieve CloudFront BytesDownloaded. Valid CloudFront Distribution ID with data showing from the CloudFront console. But lambda returned MetricDataResults with empty values.

What am I doing wrong?

Thanks!

Karl

Results from lambda's CloudWatch log:

[INFO] 2022-07-11T19:18:28.793Z 68cbba6b-5aa4-4f8d-b0d8-8fbe7abf185b CloudFront bytesdownloaded for distribution <distribution id> from 2022-06-11 19:18:28 to 2022-07-11 19:18:28: { "MetricDataResults": [ { "Id": "CloudFrontBytesDownloaded", "Label": "BytesDownloaded", "Timestamps": [], "Values": [], "StatusCode": "Complete" } ], "Messages": [], "ResponseMetadata": { "RequestId": "<request id>", "HTTPStatusCode": 200, "HTTPHeaders": { "x-amzn-requestid": "<x amen request id>", "content-type": "text/xml", "content-length": "528", "date": "Mon, 11 Jul 2022 19:18:28 GMT" }, "RetryAttempts": 0 } }

------------------------ lambda code ----------------------

import json, boto3, logging, datetime

logger = logging.getLogger() logger.setLevel(logging.INFO)

def lambda_handler(event, context):

client = boto3.client('cloudwatch')

endTime = datetime.datetime.now()
startTime = endTime - datetime.timedelta(days=30)

response = client.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'CloudFrontBytesDownloaded',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/CloudFront',
                    'MetricName': 'BytesDownloaded',
                    'Dimensions': [
                        {
                            'Name': 'DistributionId',
                            'Value': '<valid distribution id>'
                        },
                        {
                            'Name': 'Region',
                            'Value': 'Global'
                        }
                    ]
                },
                'Period': 600,
                'Stat': 'Sum',
                'Unit': 'Bytes'
            },
        },
    ],
    StartTime=startTime,
    EndTime=endTime,
)


logger.info("CloudFront bytesdownloaded for distribution <valid distribution id> from " 
    + startTime.strftime("%Y-%m-%d %H:%M:%S") + " to " + endTime.strftime("%Y-%m-%d %H:%M:%S") + ": " + json.dumps(response))
json.dumps(response)

return {
    'statusCode': 200,
    'body': json.dumps('aota-cloudfront-bytesdownloaded completed successfully!')
}
1 Answers
0

Hi, I think it's because you have Unit=Bytes. The defined unit for BytesDownloaded is None. Try omitting "Unit" from your query.

From the boto3 documentation:

In a Get operation, if you omit Unit then all data that was collected with any unit is returned, along with the corresponding units that were specified when the data was reported to CloudWatch. If you specify a unit, the operation returns only data that was collected with that unit specified. If you specify a unit that does not match the data collected, the results of the operation are null. CloudWatch does not perform unit conversions.

Also make sure you're querying us-east-1 (you're probably already doing that).

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions