01 Run describe-instances command (OSX/Linux/UNIX) with custom query filters to list the IDs of the active Amazon EC2 instances available in the selected AWS cloud region:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
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):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
-------------------------
| 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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name CPUUtilization
--start-time 2016-10-04T13:16:00
--end-time 2016-10-11T13:16: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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
{
"Datapoints": [
{
"Timestamp": "2017-03-04T17:21:00Z",
"Average": 3.2085,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-04T18:21:00Z",
"Average": 4.033499999999999995,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-04T19:21:00Z",
"Average": 1.10425,
"Unit": "Percent"
},
...
{
"Timestamp": "2017-03-11T15:21:00Z",
"Average": 2.030999999999999993,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-11T16:21:00Z",
"Average": 2.02833333333333333,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-11T17:21:00Z",
"Average": 3.02783333333333333,
"Unit": "Percent"
}
],
"Label": "CPUUtilization"
}
If the average CPU usage data returned is less than 60%, the selected Amazon EC2 instance qualifies as candidate for the underused 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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name EC2MemoryUtilization
--start-time 2016-10-04T13:16:00
--end-time 2016-10-11T13:16:00
--period 3600
--namespace AWS/EC2
--statistics Average
--dimensions Name=InstanceId,Value=i-01234abcd1234abcd
06 The command output should return the memory usage data requested:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
{
"Datapoints": [
{
"Timestamp": "2017-03-04T17:25:00Z",
"Average": 5.2085,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-04T18:25:00Z",
"Average": 6.033499999999999995,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-04T19:25:00Z",
"Average": 6.10425,
"Unit": "Percent"
},
...
{
"Timestamp": "2017-03-11T15:25:00Z",
"Average": 9.030999999999999993,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-11T16:25:00Z",
"Average": 9.02833333333333333,
"Unit": "Percent"
},
{
"Timestamp": "2017-03-11T17:25:00Z",
"Average": 9.35783333333333333,
"Unit": "Percent"
}
],
"Label": "EC2MemoryUtilization"
}
If the average memory utilization data returned is less than 60%, the selected Amazon EC2 instance qualifies as candidate for the underused 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. instance CPU and memory usage), the selected Amazon EC2 instance is considered "underutilized" and can be downsized in order to stop incurring charges for that resource.
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.