AWS SDK for Java
Developer Guide

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 alarmActions method when creating an alarm.

Enable Alarm Actions

To enable alarm actions for a CloudWatch alarm, call the CloudWatchClient's enableAlarmActions with a EnableAlarmActionsRequest containing one or more names of alarms whose actions you want to enable.

Imports

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsRequest; import software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsResponse;

Code

CloudWatchClient cw = CloudWatchClient.builder().build(); EnableAlarmActionsRequest request = EnableAlarmActionsRequest.builder() .alarmNames(alarm).build(); EnableAlarmActionsResponse response = cw.enableAlarmActions(request);

See the complete example on GitHub.

Disable Alarm Actions

To disable alarm actions for a CloudWatch alarm, call the CloudWatchClient's disableAlarmActions with a DisableAlarmActionsRequest containing one or more names of alarms whose actions you want to disable.

Imports

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.DisableAlarmActionsRequest; import software.amazon.awssdk.services.cloudwatch.model.DisableAlarmActionsResponse;

Code

CloudWatchClient cw = CloudWatchClient.builder().build(); DisableAlarmActionsRequest request = DisableAlarmActionsRequest.builder() .alarmNames(alarmName).build(); DisableAlarmActionsResponse response = cw.disableAlarmActions(request);

See the complete example on GitHub.

More Information