AWS SDK for JavaでCloudWatchのアラームを設定する方法

  private static void createCloudWatchAlerm(DBInstance instance, String alarmName, String instance) {
    AmazonCloudWatch cw = CloudWatch.getClient();
    DescribeAlarmsResult alarms =
      cw.describeAlarms(new DescribeAlarmsRequest().withAlarmNames(alarmName));
//同名のアラームが設定されてなかったら作成する
    if (alarms.getMetricAlarms().isEmpty()) {
      Dimension dimension =
        new Dimension().withName("InstanceId").withValue(
          instance);
      PutMetricAlarmRequest request =
        new PutMetricAlarmRequest()
          .withAlarmName(alarmName)
          .withComparisonOperator(
            ComparisonOperator.GreaterThanOrEqualToThreshold) // >=
          .withEvaluationPeriods(1)
          .withMetricName(instance)
          .withNamespace("YOURNAMESPACE")
          .withPeriod(300) //間隔 5 分間
          .withStatistic(Statistic.Average) //統計 平均
          .withThreshold(25.0) // しきい値
          .withActionsEnabled(false) // アクションの設定
          .withUnit(StandardUnit.Seconds)
          .withDimensions(dimension);

      cw.putMetricAlarm(request);
    }

  }

参考

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/cloudwatch/src/main/java/aws/example/cloudwatch/PutMetricAlarm.java