Getting CloudFront metrics using CloudWatch client and API

0

In my lambda function Im looking to create a monthly bytes downloaded metric, by summing up the BytesDownloaded metric from CloudFront, for each month. My problem is that the API call retrieving the data for BytesDownloaded gives an empty return, even if I can see in the CloudWatch console that this should not be the case.

The lambda is created in eu-north-1, so I tried to create the a CloudWatchClient for us-east-1, but the API call return is still empty.

I’m using client and APIs: const { CloudWatchClient, PutMetricDataCommand, GetMetricStatisticsCommand } = require('@aws-sdk/client-cloudwatch');

Im creating a command using GetMetricStatisticsCommand with params:

  • namespace: AWS/Cloudfront
  • metricname: BytesDownloaded
  • dimentions: Region:Global, DistributionId:<some-id>
  • starttime: 1st day of the month
  • endtime: last day of the month
  • period: 86400
  • statistics: ['Sum']

Then I’m sending this command using the CloudWatchClient, but the returned data is just empty.

What I'm I missing, since the return is always empty?

View from CloudWatch Metric: Enter image description here

3 Answers
1
Accepted Answer

So the problem was something as stupid as the namespace being AWS/Cloudfront without a capital F, like AWS/CloudFront.

answered 2 months ago
1

There are a few key points to consider regarding your issue with retrieving CloudFront metrics using the CloudWatch API:

  1. 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.

  2. 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.

  3. Namespace: Ensure that the namespace is exactly "AWS/CloudFront" (case-sensitive).

  4. 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.

  5. Statistics: For the BytesDownloaded metric, the valid statistic is "Sum", so your choice is correct.

  6. 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

profile picture
answered 2 months ago
0

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

profile pictureAWS
EXPERT
answered 2 months 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