Using alarm actions in CloudWatch - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

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.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsRequest; import software.amazon.awssdk.services.cloudwatch.model.EnableAlarmActionsResponse;

Code

public static void enableActions(CloudWatchClient cw, String alarm) { try { EnableAlarmActionsRequest request = EnableAlarmActionsRequest.builder() .alarmNames(alarm).build(); cw.enableAlarmActions(request); System.out.printf( "Successfully enabled actions on alarm %s", alarm); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

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.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.DisableAlarmActionsRequest;

Code

public static void disableActions(CloudWatchClient cw, String alarmName) { try { DisableAlarmActionsRequest request = DisableAlarmActionsRequest.builder() .alarmNames(alarmName) .build(); cw.disableAlarmActions(request); System.out.printf( "Successfully disabled actions on alarm %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

See the complete example on GitHub.

More information