I am new to AWS SDK (and nodejs) and I am trying to get cloudwatch's getMetricData to work. Im using a simple Cloudwatch query to grab the CPU utilization of an instance. But no matter what I do, I just keep getting this
{
ResponseMetadata: { RequestId: '5253fbfc-5aa2-4dfb-871b-1453f8610d80' },
MetricDataResults: [
{
Id: 'q1',
Label: 'q1',
Timestamps: [],
Values: [],
StatusCode: 'Complete',
Messages: []
}
],
Messages: [
{
Code: 'MaxQueryTimeRangeExceeded',
Value: 'Max time window exceeded for query'
}
]
}
here is my code
const AWS = require("aws-sdk");
AWS.config.loadFromPath("./config.json");
var cloudwatch = new AWS.CloudWatch({apiVersion: "2010-08-01"});
var params = {
StartTime: new Date('june 06, 2022 17:30'),
EndTime: new Date('june 06, 2022 18:00'),
MetricDataQueries: [
{
Id: 'q1',
Expression: "SELECT AVG(CPUUtilization) FROM SCHEMA(\"AWS/EC2\", InstanceId) WHERE InstanceId = 'i-**********'",
Period: '600'
},
],
};
cloudwatch.getMetricData(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
You can only request the latest 3 hours of data using the SELECT ...
syntax.
From docs ,
Metrics Insights queries can query only the most recent three hours of metric data.
Looks like you're only getting the CPUUtilization
for a single instance. You can get that with the MetricStat
object. Something like this:
var params = {
StartTime: new Date('june 06, 2022 17:30'),
EndTime: new Date('june 06, 2022 18:00'),
MetricDataQueries: [
{
Id: 'm1',
Label: 'CPUUtilization',
MetricStat: {
Metric: {
Namespace: 'AWS/EC2',
MetricName: 'CPUUtilization',
Dimensions: [
{
Name: 'InstanceId',
Value: 'i-xxxxxxxxxx'
}
]
},
Period: '600',
Stat: 'Average'
}
},
],
};
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.