Describing, Creating, and Deleting Alarms in Amazon CloudWatch - AWS SDK for .NET (version 3)

This documentation is for version 3.0 of the AWS SDK for .NET, which is mostly centered around .NET Framework and ASP.NET 4.x, Windows, and Visual Studio.

The latest version of the documentation at https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/ is mostly centered around .NET Core and ASP.NET Core. In addition to Windows and Visual Studio, it gives equal consideration to cross-platform development.

Describing, Creating, and Deleting Alarms in Amazon CloudWatch

This .NET example show you how to:

  • Describe a CloudWatch alarm

  • Create a CloudWatch alarm based on a metric

  • Delete a CloudWatch alarm

The Scenario

An alarm watches a single metric over a time period you specify. It performs one or more actions based on the value of the metric, relative to a given threshold over a number of time periods. The following examples show how to describe, create, and delete alarms in CloudWatch using these methods of the AmazonCloudWatchClient class:

For more information about CloudWatch alarms, see Creating Amazon CloudWatch Alarms in the Amazon CloudWatch User Guide.

Prerequisite Tasks

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

Describing an Alarm

Create an AmazonCloudWatchClient instance and a DescribeAlarmsRequest object, limiting the alarms that are returned to those with a state of INSUFFICIENT_DATA. Then call the DescribeAlarms method of the AmazonCloudWatchClient object.

using (var cloudWatch = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { var request = new DescribeAlarmsRequest(); request.StateValue = "INSUFFICIENT_DATA"; request.AlarmNames = new List<string> { "Alarm1", "Alarm2" }; do { var response = cloudWatch.DescribeAlarms(request); foreach(var alarm in response.MetricAlarms) { Console.WriteLine(alarm.AlarmName); } request.NextToken = response.NextToken; } while (request.NextToken != null); }

Creating an Alarm Based on a Metric

Create an AmazonCloudWatchClient instance and a PutMetricAlarmRequest object for the parameters needed to create an alarm that is based on a metric, in this case, the CPU utilization of an Amazon EC2 instance.

The remaining parameters are set to trigger the alarm when the metric exceeds a threshold of 70 percent.

Then call the PutMetricAlarm method of the AmazonCloudWatchClient object.

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 = "INSTANCE_ID" } }, Unit = StandardUnit.Seconds } );

Deleting an Alarm

Create an AmazonCloudWatchClient instance and a DeleteAlarmsRequest object to hold the names of the alarms you want to delete. Then call the DeleteAlarms method of the AmazonCloudWatchClient object.

using (var cloudWatch = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { var response = cloudWatch.DeleteAlarms( new DeleteAlarmsRequest { AlarmNames = new List<string> { "Alarm1", "Alarm2" }; }); }