title: CloudWatch Alarm go into ALARM for a single data point
body: <p>I am trying to create the cloudwatch ALARM if the average performance exceeds 5 milliseconds. I have configured an Amazon CloudWatch alarm to check the average value. But, the alarm is going into ALARM if just one data point breaches the threshold.</p>
  
  <pre><code>public static void main(String[] args) {
     AWSExample aws = new AWSExample();
     aws.testMethod();
  }
  </code></pre>
  
  <p>Here is the testMethod.</p>
  
  <pre><code>public void testMethod() {
          Instant start = Instant.now();
          try {
              try {
              long myValue = (long) ((Math.random())*10000);
              if(myValue&gt;8000){
                  myValue = myValue - 3000;
              }
              Thread.sleep(myValue);
              } catch(InterruptedException ex) {
                  Thread.currentThread().interrupt();
              }
          } catch (Throwable t) {
              t.printStackTrace();
          } finally {
              Instant end = Instant.now();
              Duration nano = Duration.between(start, end);
              long endTime = nano.toMillis();
              createMetricData(endTime);
              createAnAlarm();
          }
      }
  </code></pre>
  
  <p>Method to create metric data</p>
  
  <pre><code>public void createMetricData(Long metricValue) {
          final AmazonCloudWatch cw = getAmazonCloudWatch();
          Dimension dimension = new Dimension().withName("UNIQUE_METHOD").withValue("testMethod");
          MetricDatum datum = new MetricDatum()
                  .withMetricName("Method Execution Performance")
                  .withUnit(StandardUnit.Milliseconds).withValue(metricValue.doubleValue())
                  .withDimensions(dimension)
                  .withTimestamp(new Date());
          PutMetricDataRequest metricDataRequest = new PutMetricDataRequest()
                  .withNamespace("METHOD/TRAFFIC").withMetricData(datum);
          PutMetricDataResult response = cw.putMetricData(metricDataRequest);
          System.out.println(response);
          System.out.printf("Successfully put data point %f", metricValue.doubleValue());
      }
  </code></pre>
  
  <p>Here is the method which creates the alarm.</p>
  
  <pre><code>private void createAnAlarm(){
          final AmazonCloudWatch cw = getAmazonCloudWatch();
          PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest()
          .withPeriod(120)// The period, in seconds, over which the specified statistic is applied. Valid values are 10, 30, and any multiple of 60.
          .withMetricName("Method Execution Performance")// The name for // the metric // associated // with the // alarm.
          .withNamespace("METHOD/TRAFFIC")// The namespace for the metric // associated with the alarm. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html
          .withAlarmName("aws-Method-Performance")// The name for the alarm. // This name must be unique // within the AWS account.
          .withEvaluationPeriods(1)// The number of periods over which // data is compared to the specified // threshold. An alarm's total // current evaluation period can be // no longer than one day, so this
                                      // number multiplied by period cannot be more than 86,400 seconds.
          .withActionsEnabled(true)// Indicates whether actions should be executed during any changes to
                                      // the alarm state.
          .withStatistic(Statistic.Average)// The statistic for the metric associated with the alarm, other than percentile
          .withThreshold(5.0)// The value against which the specified statistic is compared.
          .withComparisonOperator(ComparisonOperator.GreaterThanThreshold)
          .withAlarmDescription("Alarm when method execution time exceeds 5 milliseconds")
          .withAlarmActions("arn:aws:sns:eu-west-1:***********")//The actions to
          //execute when this alarm transitions to the ALARM state from
          // any other state. Each action is specified as an Amazon
          // Resource Name (ARN).
          .withUnit(StandardUnit.Milliseconds)// The unit of measure for
                                                  // the statistic
          .withDimensions(new Dimension().withName("UNIQUE_METHOD").withValue("testMethod"));
          PutMetricAlarmResult result = cw.putMetricAlarm(putMetricAlarmRequest);
          System.out.println(result);
      }
  </code></pre>
  
  <p>I want the average performance of the method exceeds 5 milliseconds.
  What is the issue here and how do I solve it?</p>