AWS CloudFront bytesdownloaded metric

I have an AWS lambda written in Python 3.9 using boto3 SDK to retrieve the number of bytes downloaded from a particular AWS CloudFront Distribution Id.

The results returned empty values. What am I doing wrong.

The distribution id is valid. The lambda is in us-east-1 region where everything CloudFront is.

Thanks!

Karl``Preformatted text`“MetricDataResults”: [
{
“Id”: “aotaTbmCloudFrontBytesDownloaded”,
“Label”: “BytesDownloaded”,
“Timestamps”: ,
“Values”: ,
“StatusCode”: “Complete”
}
],
“Messages”: ,
“ResponseMetadata”: {
“RequestId”: “abcdefg”,
“HTTPStatusCode”: 200,
“HTTPHeaders”: {
“x-amzn-requestid”: “ijklmnp”,
“content-type”: “text/xml”,
“content-length”: “528”,
“date”: “Mon, 11 Jul 2022 15:41:49 GMT”
},
“RetryAttempts”: 0
}
}
from datetime import datetime

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

def lambda_handler(event, context):

client = boto3.client('cloudwatch')

response = client.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'aotaTbmCloudFrontBytesDownloaded',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/CloudFront',
                    'MetricName': 'BytesDownloaded',
                    'Dimensions': [
                        {
                            'Name': 'DistributionId',
                            'Value': 'ABCDEFG'
                        },
                    ]
                },
                'Period': 600,
                'Stat': 'Sum',
                'Unit': 'None'
            },
        },
    ],
    StartTime=datetime.fromisoformat('2022-06-11 11:13:00.000'),
    EndTime=datetime.fromisoformat('2022-07-11 11:13:00.000'),
)

logger.info("CloudFront bytesdownloaded for distribution ABCDEFBG: " + json.dumps(response))
json.dumps(response)

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

You can request support from AWS by updating your support subscription. They are pretty fast and providing good points. You won’t be able to find any “free” support here or elsewhere.