I am trying to get metric data from MetricDataQueries
in Lambda using CloudWatch Agent.
metrics_response = cw.get_metric_data(
MetricDataQueries = metrics_data_queries,
StartTime = datetime.now()+timedelta(minutes=-60),
EndTime=datetime.now()
)
The metrics_data_queries
JSON is as below.
[{"Id": "cpu_time_idle", "MetricStat": {"Metric": {"Namespace": "CWAgent", "MetricName": "cpu_time_idle",
"Dimensions": [{"Name": "InstanceId", "Value": "i-XXXX"}, {"Name": "cpu", "Value": "cpu0"}]}, "Period": 6000, "Stat": "Average"},
"Label": "cpu_time_idleResponse", "ReturnData": True},
{"Id": "cpu_time_idle", "MetricStat": {"Metric": {"Namespace": "CWAgent", "MetricName": "cpu_time_idle",
"Dimensions": [{"Name": "InstanceId", "Value": "i-XXXX"}]},
"Period": 6000, "Stat": "Average"}, "Label": "cpu_time_idleResponse", "ReturnData": True},
{"Id": "cpu_time_active", "MetricStat": {"Metric": {"Namespace": "CWAgent", "MetricName": "cpu_time_active",
"Dimensions": [{"Name": "InstanceId", "Value": "XXXX"}, {"Name": "cpu", "Value": "cpu0"}]},
"Period": 6000, "Stat": "Average"}, "Label": "cpu_time_activeResponse", "ReturnData": True},
{"Id": "cpu_time_active", "MetricStat": {"Metric": {"Namespace": "CWAgent", "MetricName": "cpu_time_active",
"Dimensions": [{"Name": "InstanceId", "Value": "i-XXXX"}]},
"Period": 6000, "Stat": "Average"}, "Label": "cpu_time_activeResponse", "ReturnData": True}]
The Error Says :-
[ERROR] ClientError: An error occurred (ValidationError) when calling the GetMetricData operation: **The values for parameter id in MetricDataQueries are not unique.**
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 27, in lambda_handler
metrics_response = get_metrics(metrics_list_response,cw)
File "/var/task/lambda_function.py", line 81, in get_metrics
metrics_response = cw.get_metric_data(
File "/var/runtime/botocore/client.py", line 391, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 719, in _make_api_call
raise error_class(parsed_response, operation_name)
Looks like because of duplicate Id, the get_metric_data
is failing. How to avoid the duplicate Ids ? This metrics are derived from a CWAgent using below config.json
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"aggregation_dimensions": [
[
"InstanceId"
]
],
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"metrics_collected": {
"collectd": {
"metrics_aggregation_interval": 60
},
"disk": {
"measurement": [
"total",
"used",
"free",
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"/"
]
},
"cpu": {
"measurement": [
"time_active",
"time_idle",
"usage_active",
"usage_idle",
"usage_user",
"usage_system"
],
"totalcpu": false,
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"total",
"used",
"active",
"available",
"available_percent",
"free",
"inactive",
"used_percent",
"cached"
],
"metrics_collection_interval": 60
},
"processes": {
"measurement": [
"total",
"running",
"sleeping",
"blocked",
"dead"
],
"metrics_collection_interval": 60
}
}
}
}
It's complaining about the Id
parameters "Id": "cpu_time_idle"
and "Id": "cpu_time_active"
. They need to be unique but the first two are the same and the second two are the same.
You can just change that in the request to cpu_time_idle_1
, cpu_time_idle_2
, cpu_time_active_1
and cpu_time_active_2
. Ids don't need to be the same as metric names, you can keep the metric names as they are and just change the Ids.