Collecting EC2 metrics using AWS CloudWatch

You can collect basic metrics such as CPU utilization, network I/O, and disk I/O metrics from your EC2 instances. There are two types of CloudWatch monitoring services—basic and detailed. In basic monitoring, the Amazon EC2 metrics data is collected at 5 minute periods and retained for 2 weeks, at no charge. The metrics are preselected and limited in number. Basic monitoring is automatically enabled for all EC2 instances. In detailed monitoring, the metrics are collected at 1-minute intervals and charged per hour per instance.

Regardless of the monitoring type, the metrics are aggregated by autoscaling group and EBS (if you are using autoscaling or EBS). If you want detailed monitoring, you can enable it, both while creating a new EC2 instance and for existing EC2 instances. You can access the metrics data using either the CloudWatch API or the AWS Management Console.

How to do it…

  1. Enable detail monitoring when launching an instance.

    Execute the following command to create an EC2 instance with detail monitoring enabled. Create VPC, subnet, security group, and key/pair before running the following command:

    $ aws ec2 run-instances 
    --image-id ami-7e2c612c 
    --count 1 
    --instance-type t1.micro 
    --key-name ApacheServerKeyPair 
    --security-group-ids sg-f332ea96 
    --subnet-id subnet-5314c936 
    --monitoring Enabled=value??
    
  2. List metrics.

    Execute the following command to list the metrics you are collecting for AWS EC2:

    $ aws cloudwatch list-metrics --namespace AWS/EC2
    
  3. Get metric statistics.

    Execute the following command to retrieve specific statistics, for example, the average CPU utilization of our EC2 instance (i-54cfb999):

    $ aws cloudwatch get-metric-statistics 
    --metric-name CPUUtilization 
    --start-time 2015-04-09T15:00:00 
    --end-time 2015-04-09T16:00:00 
    --period 300 
    --namespace AWS/EC2 
    --statistics Average 
    --dimensions Name=InstanceId,Value=i-54cfb999
    

How it works…

In the first step, we enable detail monitoring for our EC2 instance while creating it, by specifying the monitoring parameter. In AWS CloudWatch, namespaces are containers for metrics. All AWS services that provide Amazon CloudWatch data use a namespace string, beginning with AWS/. For example, the namespace for EC2 is AWS/EC2. Metrics in different namespaces are isolated from each other.

Dimension is a name/value pair that uniquely identifies a metric. AWS services that provide AWS CloudWatch data also attach dimensions to each metric. You can use the dimensions for EC2 instances, for example, ImageId, InstanceId, InstanceType, and so on, to refine or filter the metrics returned.

The list-metrics command returns a list of metrics stored for the AWS account. The output of the list-metrics command contains a token to paginate through the results. The namespace parameter in the list-metrics command helps in filtering the results for EC2 instances.

After listing the metrics, we retrieve statistical data for a given metric (using the get-metrics-statistics command) based on the namespace, metric, start and end times, period or granularity of the data points (in seconds), the return values, and the dimensions. In our example, we compute the average CPU utilization for a specific instance.

Note

For a list of namespaces, refer to the following link:

http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/aws-namespaces.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset