01 Run describe-instances command (OSX/Linux/UNIX) with custom query filters to list the IDs of all the active Amazon EC2 instances available in the selected AWS cloud region:
aws ec2 describe-instances
--region us-east-1
--filters Name=instance-state-name,Values=running
--output table
--query 'Reservations[*].Instances[*].InstanceId'
02 The command output should return a table with the requested instance identifiers (IDs):
-------------------------
| DescribeInstances |
+-----------------------+
| i-01234abcd1234abcd |
| i-0abcdabcdabcdabcd |
| i-0abcd1234abcd1234 |
+-----------------------+
03 Run get-metric-statistics command (OSX/Linux/UNIX) to get the utilization data recorded by Amazon CloudWatch for the CPUUtilization metric, representing the CPU usage of the selected Amazon EC2 instance. Change the --start-time (start recording date) and --end-time (stop recording date) parameters values to choose your own time frame for recording the instance CPU usage. Configure the --period parameter value to define the granularity (in seconds) of the returned datapoints. A period can be as short as one minute (60 seconds) or as long as one day (86400 seconds). The following command example returns the average CPU usage of an Amazon EC2 instance identified by the ID i-01234abcd1234abcd, usage data captured over a period of 7 days, using 1-hour period as the granularity for the returned datapoints:
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name CPUUtilization
--start-time 2017-04-21T15:10:00
--end-time 2017-04-28T15:10:00
--period 3600
--namespace AWS/EC2
--statistics Average
--dimensions Name=InstanceId,Value=i-01234abcd1234abcd
04 The command output should return the CPU usage details requested:
{
"Datapoints": [
{
"Timestamp": "2017-04-21T14:21:00Z",
"Average": 153.2085333333333333,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-21T15:21:00Z",
"Average": 137.03345,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-21T16:21:00Z",
"Average": 131.4999999999999993,
"Unit": "Percent"
},
...
{
"Timestamp": "2017-04-28T12:21:00Z",
"Average": 312.0365,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-28T13:21:00Z",
"Average": 290.0283,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-28T14:21:00Z",
"Average": 227.0278,
"Unit": "Percent"
}
],
"Label": "CPUUtilization"
}
If the average CPU usage data returned by the
get-metric-statistics command output is above 90%, the selected Amazon EC2 instance qualifies as candidate for the overused EC2 instance.
05 Determine the Amazon EC2 instance memory usage by querying the EC2MemoryUtilization metric data (or whatever name you have used for your custom metric) reported by the Amazon CloudWatch script installed on the selected EC2 instance (this rule assumes that the script has been successfully installed and it has recorded memory usage data within the past 7 days). To check the instance memory usage reported by your custom Amazon CloudWatch metric, run get-metric-statistics command (OSX/Linux/UNIX) using the metric name as the identifier parameter. The following command example returns the average memory utilization for an Amazon EC2 instance identified by the ID i-01234abcd1234abcd, from the usage data captured by a metric named EC2MemoryUtilization over a period of 7 days, using 1-hour period as the granularity for the returned datapoints:
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name EC2MemoryUtilization
--start-time 2017-04-21T15:10:00
--end-time 2017-04-28T15:10:00
--period 3600
--namespace AWS/EC2
--statistics Average
--dimensions Name=InstanceId,Value=i-01234abcd1234abcd
06 The command output should return the memory usage details requested:
{
"Datapoints": [
{
"Timestamp": "2017-04-21T15:10:00Z",
"Average": 97.2085,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-21T16:10:00Z",
"Average": 95.0334,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-21T17:10:00Z",
"Average": 95.1062,
"Unit": "Percent"
},
...
{
"Timestamp": "2017-04-28T13:10:00Z",
"Average": 98.03999999999999993,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-28T14:10:00Z",
"Average": 98.02833333333333333,
"Unit": "Percent"
},
{
"Timestamp": "2017-04-28T15:10:00Z",
"Average": 93.18783333333333333,
"Unit": "Percent"
}
],
"Label": "EC2MemoryUtilization"
}
If the average memory utilization recorded in the past 7 days is more than 90%, the selected Amazon EC2 instance qualifies as candidate for the overused EC2 instance.
07 If the usage data returned for the steps no. 3 – 6 satisfy all the conditions required by the conformity rule (i.e. average CPU and memory usage above 90%), the selected Amazon EC2 instance is considered "overutilized" and should be upgraded to a better hardware configuration in order to meet your workload needs.
08 Repeat steps no. 3 – 7 for each Amazon EC2 instance available in the selected AWS region.
09 Change the AWS cloud region by updating the --region command parameter value and repeat the audit process for other regions.