- Newest
- Most votes
- Most comments
So the problem was something as stupid as the namespace being AWS/Cloudfront without a capital F, like AWS/CloudFront.
There are a few key points to consider regarding your issue with retrieving CloudFront metrics using the CloudWatch API:
-
Region: You're on the right track by creating the CloudWatchClient for the us-east-1 region. CloudFront is a global service, and its metrics are sent to the US East (N. Virginia) region (us-east-1). Even though your Lambda function is in eu-north-1, you must use us-east-1 to access CloudFront metrics.
-
Dimensions: In your GetMetricStatisticsCommand parameters, you're using "Region:Global" as one of the dimensions. This is incorrect. CloudFront metrics have two dimensions: "DistributionId" and "FunctionName" (for functions only). The "Region" is not a dimension for CloudFront metrics. Remove the "Region:Global" dimension and only use "DistributionId" with your distribution ID.
-
Namespace: Ensure that the namespace is exactly "AWS/CloudFront" (case-sensitive).
-
Time Range: Make sure that the time range you're querying (starttime and endtime) aligns with when the data was actually collected. CloudWatch retains metric data for different periods based on the data's resolution. For example, 1-minute data points are available for 15 days, while 5-minute data points are available for 63 days.
-
Statistics: For the BytesDownloaded metric, the valid statistic is "Sum", so your choice is correct.
-
Period: Your period of 86400 seconds (1 day) is fine, but be aware that data aggregation affects the availability of metrics over time.
Here's a corrected version of your parameters:
- namespace: "AWS/CloudFront"
- metricName: "BytesDownloaded"
- dimensions: [{ Name: "DistributionId", Value: "<your-distribution-id>" }]
- startTime: (1st day of the month)
- endTime: (last day of the month)
- period: 86400
- statistics: ["Sum"]
If after making these corrections you still receive an empty response, double-check that:
- The distribution ID is correct
- There is actually data for the time range you're querying
- Your IAM permissions allow access to CloudWatch metrics for CloudFront
Remember, CloudFront metrics are available in CloudWatch, but you must use the US East (N. Virginia) region to access them, regardless of where your Lambda function or CloudFront distribution is located.
Sources
Get metrics by using the Amazon CloudWatch API - Amazon CloudFront
View CloudFront and edge function metrics - Amazon CloudFront
CloudWatchClient (AWS SDK for Java - 2.21.29)
Monitor CloudFront metrics with Amazon CloudWatch - Amazon CloudFront
Hi,
I would suggest that you start by using the CLI to do ths same: https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html
You will be able to check very rapidly which parameter values are correct and then you can retrofit to your Lambda code.
Start with the mandatory parameters only and then add your optional parameter like dimensions and statistics. You should the exact name of the optional parameter value in the results of the command having only the mandatory params.
The mandatory parameters are the ones below:
get-metric-statistics
--namespace <value>
--metric-name <value>
--start-time <value>
--end-time <value>
--period <value>
Best,
Didier
Relevant content
- Accepted Answerasked 2 years ago
- Accepted Answerasked 10 months ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 4 months ago