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!')
}