Developer Guide

Using Alarms in Amazon CloudWatch

This .NET example shows you how to change the state of your Amazon EC2 instances automatically based on a CloudWatch alarm.

The Scenario

Using alarm actions, you can create alarms that automatically stop, terminate, reboot, or recover your Amazon EC2 instances. You can use the stop or terminate actions when you no longer need an instance to be running. You can use the reboot and recover actions to automatically reboot those instances.

In this example, .NET is used to define an alarm action in CloudWatch that triggers the reboot of an Amazon EC2 instance. The methods use the AWS SDK for .NET to manage Amazon EC2 instances using these methods of the AmazonCloudWatchClient class:

For more information about CloudWatch alarm actions, see Create Alarms to Stop, Terminate, Reboot, or Recover an Instance in the Amazon CloudWatch User Guide.

Prerequisite Tasks

To set up and run this example, you must first:

Create and Enable Actions on an Alarm

  1. Create an AmazonCloudWatchClient instance and a PutMetricAlarmRequest object to hold the parameters for creating an alarm, specifying ActionsEnabled as true and an array of ARNs for the actions the alarm will trigger. Call the PutMetricAlarm method of the AmazonCloudWatchClient object, which creates the alarm if it doesn't exist or updates it if the alarm does exist.

    using (var client = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { client.PutMetricAlarm(new PutMetricAlarmRequest { AlarmName = "Web_Server_CPU_Utilization", ComparisonOperator = ComparisonOperator.GreaterThanThreshold, EvaluationPeriods = 1, MetricName = "CPUUtilization", Namespace = "AWS/EC2", Period = 60, Statistic = Statistic.Average, Threshold = 70.0, ActionsEnabled = true, AlarmActions = new List<string> { "arn:aws:swf:us-west-2:" + "customerAccount" + ":action/actions/AWS_EC2.InstanceId.Reboot/1.0" }, AlarmDescription = "Alarm when server CPU exceeds 70%", Dimensions = new List<Dimension> { new Dimension { Name = "InstanceId", Value = "instanceId" } }, Unit = StandardUnit.Seconds }); }
  2. When PutMetricAlarm completes successfully, create an EnableAlarmActionsRequest object containing the name of the CloudWatch alarm. Call the EnableAlarmActions method to enable the alarm action.

    client.EnableAlarmActions(new EnableAlarmActionsRequest { AlarmNames = new List<string> { "Web_Server_CPU_Utilization" } });
  3. Create a MetricDatum object containing the CPUUtilization custom metric. Create a PutMetricDataRequest object containing the MetricData parameter needed to submit a data point for the CPUUtilization metric. Call the PutMetricData method.

    MetricDatum metricDatum = new MetricDatum { MetricName = "CPUUtilization" }; PutMetricDataRequest putMetricDatarequest = new PutMetricDataRequest { MetricData = new List<MetricDatum> { metricDatum } }; client.PutMetricData(putMetricDatarequest);

Disable Actions on an Alarm

Create an AmazonCloudWatchClient instance and a DisableAlarmActionsRequest object containing the name of the CloudWatch alarm. Call the DisableAlarmActions method to disable the actions for this alarm.

using (var client = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { client.DisableAlarmActions(new DisableAlarmActionsRequest { AlarmNames = new List<string> { "Web_Server_CPU_Utilization" } }); }