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:
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 --periodparameter 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 (set by the--start-time and --end-time command parameters), using 1-hour period as the granularity for the returned datapoints (set by the --period parameter):
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:
{
"Datapoints": [
{
"Timestamp": "2016-10-04T13:16:00Z",
"Average": 0.2085,
"Unit": "Percent"
},
{
"Timestamp": "2016-10-04T14:16:00Z",
"Average": 0.033499999999999995,
"Unit": "Percent"
},
{
"Timestamp": "2016-10-04T15:16:00Z",
"Average": 0.10425,
"Unit": "Percent"
},
...
{
"Timestamp": "2016-10-11T10:16:00Z",
"Average": 0.030999999999999993,
"Unit": "Percent"
},
{
"Timestamp": "2016-10-11T11:16:00Z",
"Average": 0.02833333333333333,
"Unit": "Percent"
},
{
"Timestamp": "2016-10-11T12:16:00Z",
"Average": 0.02783333333333333,
"Unit": "Percent"
}
],
"Label": "CPUUtilization"
}
If the average CPU usage data returned is less than 2%, the selected Amazon EC2 instance qualifies as candidate for the idle EC2 instance.
05 Run get-metric-statistics command (OSX/Linux/UNIX) to get the utilization data recorded for the NetworkIn metric, representing the incoming network traffic for the selected Amazon EC2 instance. The following command example returns the average incoming network traffic received by an Amazon EC2 instance identified by the ID i-01234abcd1234abcd, usage data captured over a period of 7 days (set by the --start-time and --end-time parameters), using 1-hour period as the granularity for the returned datapoints (set by the --period parameter):
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name NetworkIn
--start-time 2016-10-04T13:16:22
--end-time 2016-10-11T13:16:22
--period 3600
--namespace AWS/EC2
--statistics Average
--dimensions Name=InstanceId,Value=i-01234abcd1234abcd
06 The command output should return the incoming network traffic data requested:
{
"Datapoints": [
{
"Timestamp": "2016-10-04T13:16:22Z",
"Average": 220.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-04T14:16:22Z",
"Average": 145.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-04T15:16:22Z",
"Average": 292.0,
"Unit": "Bytes"
},
...
{
"Timestamp": "2016-10-11T10:16:22Z",
"Average": 151.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-11T11:16:22Z",
"Average": 430.0,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-11T12:16:22Z",
"Average": 2330.777777777778,
"Unit": "Bytes"
}
],
"Label": "NetworkIn"
}
If the average incoming traffic values returned by the
get-metric-statistics command output are less than 5 MB, the selected Amazon EC2 instance qualifies as candidate for the idle EC2 instance.
07 Run get-metric-statistics command (OSX/Linux/UNIX) to get the utilization data recorded by Amazon CloudWatch for the NetworkOut metric, representing the outgoing network traffic for the selected Amazon EC2 instance. The following command example returns the average outgoing network traffic sent from an Amazon EC2 instance identified by the ID i-01234abcd1234abcd, usage data captured over a period of 7 days (set by the --start-time and --end-time command parameters), using 1-hour period as the granularity for the returned datapoints (set by the --period parameter):
aws cloudwatch get-metric-statistics
--region us-east-1
--metric-name NetworkOut
--start-time 2016-10-04T13:16:58
--end-time 2016-10-11T13:16:58
--period 3600
--namespace AWS/EC2
--statistics Average
--dimensions Name=InstanceId,Value=i-01234abcd1234abcd
08 The command output should return the outgoing network traffic data requested:
{
"Datapoints": [
{
"Timestamp": "2016-10-04T13:16:58Z",
"Average": 148.65,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-04T14:16:58Z",
"Average": 91.63333333333334,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-04T15:16:58Z",
"Average": 214.96666666666667,
"Unit": "Bytes"
},
...
{
"Timestamp": "2016-10-11T10:16:58Z",
"Average": 96.83333333333333,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-11T11:16:58Z",
"Average": 325.96666666666664,
"Unit": "Bytes"
},
{
"Timestamp": "2016-10-11T13:12:58Z",
"Average": 2656.488888888889,
"Unit": "Bytes"
}
],
"Label": "NetworkOut"
}
If the average outgoing traffic values returned are less than 5 MB, the selected Amazon EC2 instance qualifies as candidate for the idle EC2 instance.
09 Run describe-tags command (OSX/Linux/UNIX) to describe the tag sets defined for the selected Amazon EC2 instance:
aws ec2 describe-tags
--region us-east-1
--filters "Name=resource-id,Values=i-01234abcd1234abcd"
10 The command output should return the tags (key-value pairs) applied to the specified EC2 instance. The Roleand Ownertags returned and their values (highlighted) can be used to determine the EC2 resource role within your application stack and to contact its owner for more information in order to decide whether the selected Amazon EC2 instance can be stopped or terminated:
{
"Tags": [
{
"ResourceType": "instance",
"ResourceId": "i-01234abcd1234abcd",
"Value": "ops@cloudconformity.com",
"Key": "Owner"
},
{
"ResourceType": "instance",
"ResourceId": "i-01234abcd1234abcd",
"Value": "legacy-webapp-ver-1.5",
"Key": "Name"
},
{
"ResourceType": "instance",
"ResourceId": "i-01234abcd1234abcd",
"Value": "legacy-webapp-test-server",
"Key": "Role"
}
]
}
11 If the data returned for the steps no. 3 – 10 satisfy all the conditions required by the conformity rule (i.e. instance role, owner, CPU, and network usage), the selected Amazon EC2 instance is considered "idle" and can be stopped or terminated in order to stop incurring charges for that resource.
12 Repeat steps no. 3 – 11 for each Amazon EC2 instance available in the selected AWS region.
13 Change the AWS cloud region by updating the --regioncommand parameter value and repeat the audit process for other regions.