AWS CLI combine ECS describe-tasks and Cloudwatch metrics statistics - amazon-web-services

I have successfully output the cpu and memory allocated for each task in my cluster using the below cli command. I'm also trying to get average actual cpu utilization from cloudwatch all on same query but with little luck. Ideally I will like the field list to have group, cpu, memory, averagecpuUsed.
This gives me the list of clusters:
aws ecs describe-tasks --cluster mycluster --output text --query 'tasks[*] | [].[group,cpu,memory]' --tasks `aws ecs list-tasks --desired-status RUNNING --query taskArns --cluster mycluster --output text`
I'm trying to combine with this:
aws cloudwatch get-metric-statistics --namespace ECS/ContainerInsights --metric-name CpuUtilized --start-time 2023-01-18T00:01:00Z --end-time 2023-01-18T23:59:00Z --period 86400 --dimensions Name=ClusterName, Value=mycluster Name=ServiceName, Value=myservice --statistics Average

Related

how to get cloudwatch metrics from cli

I am trying to write my own tools for AWS monitoring calling AWS api from console and later build some graphs.
aws cloudwatch get-metric-statistics --metric-name MemoryUtilization --start-time 2022-11-19 --end-time 2022-11-21 --period 3600 --dimensions Name=ClusterName,Value=<mycluster> --dimensions Name=ServiceName,Value=<service-name> --namespace ECS/ContainerInsights --statistics Average
and this is always reporting 0 datapoint, whereas I can see that there are metrics collected in the console.
what is wrong with the command?
issue was the multiple dimensions args are given by just separating then with a space, instead of adding another --dimensions
so this worked.
echo running $metric on $ServiceName
aws cloudwatch get-metric-statistics --metric-name $metric \
--start-time $start_time --end-time $end_time \
--period $period \
--dimensions Name=ClusterName,Value=<my-cluster> Name=ServiceName,Value=$ServiceName \
--namespace $namespace \
--statistics $statistics --output text > $ServiceName-$metric-$now.csv

How to get list of all AWS EC2 instances with specific tag and their utilisation of CPU and RAM

I am struggling to make the command to retrieve list of all AWS EC2 instances with specific tags and their CPU and RAM utilization.
Can someone help me with that ?
aws ec2 describe-instances --query 'Reservations[].Instances[].{Name: Tags[?Key==`Name`].Value | [0], Role: Tags[?Key==`Billing by Role`].Value | [0]}' --output text
aws ec2 describe-instances --query 'Reservations[].Instances[].{Name: Tags[?Key==`Name`].Value | [0], Role: Tags[?Key==`Billing by Role`].Value | [0]}' --output text
It gives me list of all the instances but not sure for utilization.
You can get these metrics from cloudwatch, so get all the instance with tag first then run the loop to get stats of each instance.
#!/bin/bash
filter="prod"
AWS_INSTANCE_WITH_ID=$(aws ec2 describe-instances --filter Name=tag:Name,Values="${filter}" --query "Reservations[*].Instances[*].[InstanceId]" --output text)
for instance_id in $AWS_INSTANCE_WITH_ID
do
# Now get CPU against instance ID from cloud metrics one by one against that tag
aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2019-09-12T23:18:00Z --end-time 2019-09-13T23:18:00Z --period 3600 --namespace AWS/EC2 --statistics Maximum --dimensions Name=InstanceId,Value=$instance_id
done
Amazon CloudWatch retains metric data as follows:
Data points with a period of less than 60 seconds are available for 3
hours. These data points are high-resolution metrics and are available
only for custom metrics that have been defined with a
StorageResolution of 1. Data points with a period of 60 seconds
(1-minute) are available for 15 days. Data points with a period of 300
seconds (5-minute) are available for 63 days. Data points with a
period of 3600 seconds (1 hour) are available for 455 days (15
months).
Get-metric-statistics

How to get metrics data from aws cloudwatch to csv

i have been working on project, and i want to export metrics data from cloudwatch like CPU Utilization and Network Out data, is there any way to get that data? and convert it to csv?
Export CloudWatch Metrics
You can't do it directly but following is the step by step instructions:
Pre Reqs
AWS CLI (Command Line Interface) from here
jq is a lightweight and flexible command-line JSON processor from here
How to export
Use the following CLI:
aws cloudwatch get-metric-statistics
--namespace AWS/EC2 --metric-name CPUUtilization
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx
--statistics Average
--start-time 2020-03-01T00:00:00
--end-time 2020-03-31T23:59:00
--period 3600
--region us-east-1
Valid options for --metric-name depend on the --name-space parameter. For AWS/EC2, the full list can be seen by running the following CLI command:
aws cloudwatch list-metrics --namespace "AWS/EC2"
Valid options for --statistics are:
SampleCount
Average
Sum
Minimum
Maximum
--start-time and --end-time specify the range.
--period The granularity, in seconds, of the returned data points.
--region The region where the CloudWatch metric is being meatured (us-east-1, us-west-2 etc.)
Data output will look something like the following:
{
"Label": "CPUUtilization",
"Datapoints": []
}
To convert it to CSV we will use jq. For this you have two options:
Option 1
Pipe all the aws cli output to jq:
aws cloudwatch get-metric-statistics
--namespace AWS/EC2 --metric-name CPUUtilization
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx
--statistics Average
--start-time 2020-03-01T00:00:00
--end-time 2020-03-31T23:59:00
--period 3600
--region us-east-1
| jq -r '.Datapoints[] | [.Timestamp, .Minimum, .Unit] | #csv'
Option 2
Export the data to JSON:
aws cloudwatch get-metric-statistics
--namespace AWS/EC2 --metric-name CPUUtilization
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx
--statistics Average
--start-time 2020-03-01T00:00:00
--end-time 2020-03-31T23:59:00
--period 3600
--region us-east-1 >> data.json
Use jq to read the json to csv:
jq -r '.Datapoints[] | [.Timestamp, .Minimum, .Unit] | #csv' data.json
Output
Here is how the output will look like:
"2020-03-24T11:00:00Z",0.327868852454245,"Percent"
"2020-03-11T21:00:00Z",0.327868852454245,"Percent"
"2020-03-15T04:00:00Z",0.322580645156596,"Percent"
"2020-03-27T18:00:00Z",0.327868852478101,"Percent"
There is no in-built capability to export Amazon CloudWatch metrics to CSV.
There are API calls available to extract metrics, but you would need to write a program to call the API, receive the metrics and store it in a suitable format.
There are projects available that can assist with this, such as:
mogproject/cloudwatch-dump: Just dump all the CloudWatch metrics.
AWS – CloudWatch – DataExtractor – API | vMan
romgapuz/awsmetric2csv: AWS Metric to CSV is a Python command-line utility to extract CloudWatch metric data from an AWS resources (e.g. EC2, RDS) and save to a CSV file.
If you are looking for these types of tools, make sure they refer to CloudWatch metrics, not CloudWatch Logs (which is something different).
You can also try out official Prometheus cloudwatch_exporter

How to retrieve cloudwatch metrics from an list of instances

I have multiple instances. I need to retrieve some metrics from a subset of them (not all) using aws cli. I have tried specifying the InstanceId multiple times as Dimension, but it only considers on of the values. For example the command above, only returns de metric values for instance i-xxxxxx (ignores i-yyyyyyy)
aws cloudwatch get-metric-statistics --namespace AWS/EC2 \
--metric-name CPUUtilization \
--statistics Maximum
--dimensions Name=InstanceId,Value=i-yyyyyyyyyy Name=InstanceId,Value=i-xxxxxxxxx \
--start-time 2018-08-01T00:00:00Z --end-time 2018-08-01T10:00:00Z --period 300
One additional comment: the subset is can be obtained by filtering the list of instances using a tag:
aws ec2 describe-instances --filter Name=tag:app,Values=myapp \
--query 'Reservations[*].Instances[*].InstanceId' --output text
You're using the wrong API: get-metric-statistics is intended to return the time-series data for a single metric, which is identified by all of its dimensions. I suspect that the CLI interprets this field as an associative array, so the second instance ID overwrote the first.
The simplest solution (assuming you're using bash on Linux) is to use a for loop to retrieve each set of metrics:
for instance in i-yyyyyyyyyy i-xxxxxxxxx
do aws cloudwatch get-metric-statistics \
--dimensions "Name=InstanceId,Value=${instance}" \
...
done

Downloading datapoints by Dimension from Amazon CloudWatch

I am publishing a custom metric into CloudWatch with two dimensions:
aws cloudwatch --region ap-southeast-1 put-metric-data --namespace CustomNS --metric-name ApiReqCount --dimensions ApiName=TestAPI,ApplicationName=App1 --timestamp 2017-03-07T05:00:00.000Z --statistic-value Sum=7,Minimum=1,Maximum=7,SampleCount=1
If I query with all of the dimensions, it gives the datapoints:
aws cloudwatch --region ap-southeast-1 get-metric-statistics --namespace CustomNS --metric-name ApiReqCount --dimensions Name=ApiName,Value=TestAPI Name=ApplicationName,Value=App1 --statistics Sum --start-time 2017-03-05T00:00:00Z --end-time 2017-03-08T12:00:00Z --period 300
However when I query, and no dimensions or partial dimensions are specified, I do not get back datapoints:
aws cloudwatch --region ap-southeast-1 get-metric-statistics --namespace CustomNS --metric-name ApiReqCount --dimensions Name=ApiName,Value=TestAPI --statistics Sum --start-time 2017-03-05T00:00:00Z --end-time 2017-03-08T12:00:00Z --period 300
What I really want is that I need all datapoints returned when no dimensions are specified, I'd like to optionally filter these datapoints via Dimensions.
You can't query for partial dimension or mix dimensions even if they are identical.
From the documentation:
CloudWatch treats each unique combination of dimensions as a separate metric, even if the metrics have the same metric name. You can't retrieve statistics using combinations of dimensions that you did not specifically publish.
See examples on: Amazon CloudWatch Concepts - Dimensions