The AWS SDK for Java 1.x has entered maintenance mode as of July 31, 2024,
and will reach end-of-support
Using Alarm Actions in CloudWatch
Using CloudWatch alarm actions, you can create alarms that perform actions such as automatically stopping, terminating, rebooting, or recovering Amazon EC2 instances.
Note
Alarm actions can be added to an alarm by using the PutMetricAlarmRequest's setAlarmActions
method when creating an alarm.
Enable Alarm Actions
To enable alarm actions for a CloudWatch alarm, call the AmazonCloudWatchClient’s enableAlarmActions
with a EnableAlarmActionsRequest containing one or more names of alarms whose actions you want to enable.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.EnableAlarmActionsRequest; import com.amazonaws.services.cloudwatch.model.EnableAlarmActionsResult;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); EnableAlarmActionsRequest request = new EnableAlarmActionsRequest() .withAlarmNames(alarm); EnableAlarmActionsResult response = cw.enableAlarmActions(request);
Disable Alarm Actions
To disable alarm actions for a CloudWatch alarm, call the AmazonCloudWatchClient’s disableAlarmActions
with a DisableAlarmActionsRequest containing one or more names of alarms whose actions you want to disable.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DisableAlarmActionsRequest; import com.amazonaws.services.cloudwatch.model.DisableAlarmActionsResult;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DisableAlarmActionsRequest request = new DisableAlarmActionsRequest() .withAlarmNames(alarmName); DisableAlarmActionsResult response = cw.disableAlarmActions(request);
More Information
-
Create Alarms to Stop, Terminate, Reboot, or Recover an Instance in the Amazon CloudWatch User Guide
-
PutMetricAlarm in the Amazon CloudWatch API Reference
-
EnableAlarmActions in the Amazon CloudWatch API Reference
-
DisableAlarmActions in the Amazon CloudWatch API Reference