I am trying to get number of requests from my Cloudfront instance. Currently, I use function getMetricData
provided by aws-sdk
in Javascripts. However, I always get an response without any values when I use lambda to query for the metric data, while I can clearly see there are requests when I check the Metric page of CloudWatch on browser.
I already check CloudWatch API document . However, I am not 100% clear about the arguments that I need for getting the "requests" metric data. Here is some code similar to what I did.
const AWS = require('aws-sdk');
exports.handler = async(event) => {
// TODO implement
let endDate = "2020-03-04T13:14:18.546Z";
let startDate = "2020-02-28T13:14:18.546Z";
let cloudwatch = new AWS.CloudWatch();
var params = {
"StartTime": startDate,
"EndTime": endDate,
"MetricDataQueries": [{
"Id": "m1",
"MetricStat": {
"Metric": {
"Namespace": "AWS/CloudFront",
"MetricName": "Requests",
"Dimensions": [{
"Name": "DistributionId",
"Value": "EG12345678" <-- I made this up
}]
},
"Period": 60,
"Stat": "Sum",
"Unit": "Count"
}
}]
};
const response = await cloudwatch.getMetricData(params).promise();
return response
}
I am sure in between the start date and end data that there are more than 100 requests. However, the return values array is simply empty.
Thanks in advance
Ok, I just forget to specify region in the dimension field
here is a fixed one
const AWS = require('aws-sdk');
exports.checkHealth = async(event) => {
// TODO implement
let endDate = new Date;
let startDate = new Date(endDate - 20 * 60 * 1000);
let cloudwatch = new AWS.CloudWatch();
//let startDate = "2020-02-28T13:00:00.000Z";
//let endDate = "2020-02-28T15:30:00.000Z";
var params = {
"StartTime": startDate,
"EndTime": endDate,
"MetricDataQueries": [{
"Id": "m1",
"MetricStat": {
"Metric": {
"Namespace": "AWS/CloudFront",
"MetricName": "TotalErrorRate",
"Dimensions": [{
"Name": "DistributionId",
"Value": "EG12345678"
},
{
"Name": "Region", // <------ this fix things
"Value": "Global" // <------ this fix things
}]
},
"Period":60,
"Stat": "Average"
}
}]
};
const response = await cloudwatch.getMetricData(params).promise();
return response;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.