The AWS SDK for Java 1.x has entered maintenance mode as of July 31, 2024,
and will reach end-of-support
Working with CloudWatch Alarms
Create an Alarm
To create an alarm based on a CloudWatch metric, call the AmazonCloudWatchClient’s putMetricAlarm
method with a PutMetricAlarmRequest filled with the alarm conditions.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.ComparisonOperator; import com.amazonaws.services.cloudwatch.model.Dimension; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult; import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.cloudwatch.model.Statistic;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("InstanceId") .withValue(instanceId); PutMetricAlarmRequest request = new PutMetricAlarmRequest() .withAlarmName(alarmName) .withComparisonOperator( ComparisonOperator.GreaterThanThreshold) .withEvaluationPeriods(1) .withMetricName("CPUUtilization") .withNamespace("{AWS}/EC2") .withPeriod(60) .withStatistic(Statistic.Average) .withThreshold(70.0) .withActionsEnabled(false) .withAlarmDescription( "Alarm when server CPU utilization exceeds 70%") .withUnit(StandardUnit.Seconds) .withDimensions(dimension); PutMetricAlarmResult response = cw.putMetricAlarm(request);
List Alarms
To list the CloudWatch alarms that you have created, call the AmazonCloudWatchClient’s describeAlarms
method with a DescribeAlarmsRequest that you can use to set options for the result.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult; import com.amazonaws.services.cloudwatch.model.MetricAlarm;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; DescribeAlarmsRequest request = new DescribeAlarmsRequest(); while(!done) { DescribeAlarmsResult response = cw.describeAlarms(request); for(MetricAlarm alarm : response.getMetricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.getAlarmName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }
The list of alarms can be obtained by calling getMetricAlarms
on the DescribeAlarmsResult that is returned by describeAlarms
.
The results may be paged. To retrieve the next batch of results, call setNextToken
on the original request object with the return value of the DescribeAlarmsResult
object’s getNextToken
method, and pass the modified request object back to another call to describeAlarms
.
Note
You can also retrieve alarms for a specific metric by using the AmazonCloudWatchClient’s describeAlarmsForMetric
method. Its use is similar to describeAlarms
.
Delete Alarms
To delete CloudWatch alarms, call the AmazonCloudWatchClient’s deleteAlarms
method with a DeleteAlarmsRequest containing one or more names of alarms that you want to delete.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsResult;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DeleteAlarmsRequest request = new DeleteAlarmsRequest() .withAlarmNames(alarm_name); DeleteAlarmsResult response = cw.deleteAlarms(request);
More Information
-
Creating Amazon CloudWatch Alarms in the Amazon CloudWatch User Guide
-
PutMetricAlarm in the Amazon CloudWatch API Reference
-
DescribeAlarms in the Amazon CloudWatch API Reference
-
DeleteAlarms in the Amazon CloudWatch API Reference