Using Alarm Actions in CloudWatch - AWS SDK for Java 1.x

The AWS SDK for Java 1.x has entered maintenance mode as of July 31, 2024, and will reach end-of-support on December 31, 2025. We recommend that you migrate to the AWS SDK for Java 2.x to continue receiving new features, availability improvements, and security updates.

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