use of software.amazon.awssdk.services.cloudwatch.model.CloudWatchException in project aws-doc-sdk-examples by awsdocs.
the class PutMetricAlarm method putMetricAlarm.
// snippet-start:[cloudwatch.java2.put_metric_alarm.main]
public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) {
try {
Dimension dimension = Dimension.builder().name("InstanceId").value(instanceId).build();
PutMetricAlarmRequest request = PutMetricAlarmRequest.builder().alarmName(alarmName).comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD).evaluationPeriods(1).metricName("CPUUtilization").namespace("AWS/EC2").period(60).statistic(Statistic.AVERAGE).threshold(70.0).actionsEnabled(false).alarmDescription("Alarm when server CPU utilization exceeds 70%").unit(StandardUnit.SECONDS).dimensions(dimension).build();
cw.putMetricAlarm(request);
System.out.printf("Successfully created alarm with name %s", alarmName);
} catch (CloudWatchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.cloudwatch.model.CloudWatchException in project aws-doc-sdk-examples by awsdocs.
the class PutTargets method putCWTargets.
// snippet-start:[cloudwatch.java2.put_targets.main]
public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) {
try {
Target target = Target.builder().arn(functionArn).id(targetId).build();
PutTargetsRequest request = PutTargetsRequest.builder().targets(target).rule(ruleName).build();
PutTargetsResponse response = cwe.putTargets(request);
System.out.printf("Successfully created CloudWatch events target for rule %s", ruleName);
} catch (CloudWatchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations