Fetch multiple couldwatch metrics in one cli call

0

Hi, how do I retrieve multiple metrics of ec2 instance in a single aws cli call? I want to retrieve cpu, writeops, readops etc for an ec2 instance, but instead of of issuing multiple aws cloudwatch get-metric-statistics calls for each metric, is there a way to get data for all metrics in one call?

aws cloudwatch get-metric-statistics --metric-name EBSWriteOps --start-time "2022-04-18" --end-time "2022-04-19" --period 60 --namespace AWS/EC2 --statistics Average --dimensions Name=InstanceId,Value=$id --output text --query 'avg(Datapoints[*].Average)'

aws cloudwatch get-metric-statistics --metric-name CPUUtilization  --start-time "2022-04-18" --end-time "2022-04-19" --period 60 --namespace AWS/EC2 --statistics Average --dimensions Name=InstanceId,Value=$id --output text --query 'avg(Datapoints[*].Average)'

Can above 2 calls be combined as one call?

asked 2 years ago2138 views
1 Answer
0
Accepted Answer

You can use* aws cloudwatch get-metric-data* which will allow you to pass an array of metric data queries. (Reference - https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudwatch/get-metric-data.html)

A sample command will look like this -

COMMAND aws cloudwatch get-metric-data --cli-input-json file://input.json

INPUT FILE - input.json

{
	"MetricDataQueries": [{
			"Id": "cpuUtilization",
			"MetricStat": {
				"Metric": {
					"Namespace": "AWS/EC2",
					"MetricName": "CPUUtilization",
					"Dimensions": [{
						"Name": "InstanceId",
						"Value": "i-000000"
					}]
				},
				"Period": 60,
				"Stat": "Average"
			},
			"ReturnData": true
		},
		{
			"Id": "networkPacketsOut",
			"MetricStat": {
				"Metric": {
					"Namespace": "AWS/EC2",
					"MetricName": "NetworkPacketsOut",
					"Dimensions": [{
						"Name": "InstanceId",
						"Value": "i-000000"
					}]
				},
				"Period": 60,
				"Stat": "Average"
			},
			"ReturnData": true
		}
	],
	"StartTime": "2022-04-01T00:00:00",
	"EndTime": "2022-04-30T00:00:00"
}

SAMPLE OUTPUT

{
	"MetricDataResults": [{
			"Id": "cpuUtilization",
			"Label": "CPUUtilization",
			"Timestamps": [
				"2022-04-20T01:07:00+00:00",
				"2022-04-20T01:02:00+00:00",
				"2022-04-20T00:57:00+00:00"
			],
			"Values": [
				0.09945355191256719,
				0.13280540890988418,
				0.13450128228466757
			],
			"StatusCode": "Complete"
		},
		{
			"Id": "networkPacketsOut",
			"Label": "NetworkPacketsOut",
			"Timestamps": [
				"2022-04-20T01:07:00+00:00",
				"2022-04-20T01:02:00+00:00",
				"2022-04-20T00:57:00+00:00"
			],
			"Values": [
				82.4,
				86.6,
				82.2
			],
			"StatusCode": "Complete"
		}
	],
	"Messages": []
}
AWS
ganesh
answered 2 years 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