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!')
}
profile picture
已提問 2 年前檢視次數 264 次
1 個回答
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).

專家
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南