AWS SDK for Java
Developer Guide

Working with CloudWatch Alarms

Create an Alarm

To create an alarm based on a CloudWatch metric, call the CloudWatchClient's putMetricAlarm method with a PutMetricAlarmRequest filled with the alarm conditions.

Imports

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmResponse; import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import software.amazon.awssdk.services.cloudwatch.model.Statistic;

Code

CloudWatchClient cw = CloudWatchClient.builder().build(); Dimension dimension = Dimension.builder() .name("InstanceId") .value(instanceId).build(); PutMetricAlarmRequest request = PutMetricAlarmRequest.builder() .alarmName(alarmName) .comparisonOperator( ComparisonOperator.GREATER_THAN_THRESHOLD) .evaluationPeriods(1) .metricName("CPUUtilization") .namespace("AWS/EC2") .period(60) .statistic(Statistic.AVERAGE) .threshold(70.0) .actionsEnabled(false) .alarmDescription( "Alarm when server CPU utilization exceeds 70%") .unit(StandardUnit.SECONDS) .dimensions(dimension) .build(); PutMetricAlarmResponse response = cw.putMetricAlarm(request);

See the complete example on GitHub.

List Alarms

To list the CloudWatch alarms that you have created, call the CloudWatchClient's describeAlarms method with a DescribeAlarmsRequest that you can use to set options for the result.

Imports

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse; import software.amazon.awssdk.services.cloudwatch.model.MetricAlarm;

Code

CloudWatchClient cw = CloudWatchClient.builder().build(); boolean done = false; String new_token = null; while(!done) { DescribeAlarmsResponse response; if (new_token == null) { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder().build(); response = cw.describeAlarms(request); } else { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder() .nextToken(new_token) .build(); response = cw.describeAlarms(request); } for(MetricAlarm alarm : response.metricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.alarmName()); } if(response.nextToken() == null) { done = true; } else { new_token = response.nextToken(); } }

The list of alarms can be obtained by calling MetricAlarms on the DescribeAlarmsResponse that is returned by describeAlarms.

The results may be paged. To retrieve the next batch of results, call nextToken on the response object and use the token value to build a new request object. Then call the describeAlarms method again with the new request.

Note

You can also retrieve alarms for a specific metric by using the CloudWatchClient's describeAlarmsForMetric method. Its use is similar to describeAlarms.

See the complete example on GitHub.

Delete Alarms

To delete CloudWatch alarms, call the CloudWatchClient's deleteAlarms method with a DeleteAlarmsRequest containing one or more names of alarms that you want to delete.

Imports

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsResponse;

Code

CloudWatchClient cw = CloudWatchClient.builder().build(); DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarm_name).build(); DeleteAlarmsResponse response = cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarm_name);

See the complete example on GitHub.

More Information