# AnomalyDetectionMetricOptions

Properties needed to make an anomaly detection alarm from a metric.

Extends: [MathExpressionOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.MathExpressionOptions.html)
Used as argument in: [Metric.anomalyDetectionFor](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Metric.html#anomalydetectionfor)

- Source: [`aws-cloudwatch/lib/metric.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts#L1203)
- Type last changed: v2.197.0 · 21 May 2025

## Example

TypeScript:

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
import { aws_cloudwatch as cloudwatch } from 'aws-cdk-lib';

declare const metric: cloudwatch.Metric;
const anomalyDetectionMetricOptions: cloudwatch.AnomalyDetectionMetricOptions = {
  metric: metric,

  // the properties below are optional
  color: 'color',
  label: 'label',
  period: cdk.Duration.minutes(30),
  searchAccount: 'searchAccount',
  searchRegion: 'searchRegion',
  stdDevs: 123,
  visible: false,
};
```

Python:

```python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
import aws_cdk as cdk
from aws_cdk import aws_cloudwatch as cloudwatch

# metric: cloudwatch.Metric

anomaly_detection_metric_options = cloudwatch.AnomalyDetectionMetricOptions(
    metric=metric,

    # the properties below are optional
    color="color",
    label="label",
    period=cdk.Duration.minutes(30),
    search_account="searchAccount",
    search_region="searchRegion",
    std_devs=123,
    visible=False
)
```

Java:

```java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.cloudwatch.*;

Metric metric;

AnomalyDetectionMetricOptions anomalyDetectionMetricOptions = AnomalyDetectionMetricOptions.builder()
        .metric(metric)

        // the properties below are optional
        .color("color")
        .label("label")
        .period(Duration.minutes(30))
        .searchAccount("searchAccount")
        .searchRegion("searchRegion")
        .stdDevs(123)
        .visible(false)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK;
using Amazon.CDK.AWS.CloudWatch;

Metric metric;

var anomalyDetectionMetricOptions = new AnomalyDetectionMetricOptions {
    Metric = metric,

    // the properties below are optional
    Color = "color",
    Label = "label",
    Period = Duration.Minutes(30),
    SearchAccount = "searchAccount",
    SearchRegion = "searchRegion",
    StdDevs = 123,
    Visible = false
};
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import cdk "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var metric Metric

anomalyDetectionMetricOptions := &AnomalyDetectionMetricOptions{
	Metric: metric,

	// the properties below are optional
	Color: jsii.String("color"),
	Label: jsii.String("label"),
	Period: cdk.Duration_Minutes(jsii.Number(30)),
	SearchAccount: jsii.String("searchAccount"),
	SearchRegion: jsii.String("searchRegion"),
	StdDevs: jsii.Number(123),
	Visible: jsii.Boolean(false),
}
```

## Properties

- `color`: string — Color for this metric when added to a Graph in a Dashboard.

  **Default:** Automatic color

- `label`: string — Label for this expression when added to a Graph in a Dashboard.

  **Default:** Expression value is used as label

  If this expression evaluates to more than one time series (for
  example, through the use of `METRICS()` or `SEARCH()` expressions),
  each time series will appear in the graph using a combination of the
  expression label and the individual metric label. Specify the empty
  string (`''`) to suppress the expression label and only keep the
  metric label.

  You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html)
  to show summary information about the displayed time series
  in the legend. For example, if you use:

  ```
  [max: ${MAX}] MyMetric
  ```

  As the metric label, the maximum value in the visible range will
  be shown next to the time series name in the graph's legend. If the
  math expression produces more than one time series, the maximum
  will be shown for each individual time series produce by this
  math expression.

- `metric`: [IMetric](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.IMetric.html) (required) — The metric to add the alarm on.

  Metric objects can be obtained from most resources, or you can construct
  custom Metric objects by instantiating one.

- `period`: [Duration](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Duration.html) = Duration.minutes(5) — The period over which the math expression's statistics are applied.

  This period overrides all periods in the metrics used in this
  math expression.

- `searchAccount`: string — Account to evaluate search expressions within.

  **Default:** Deployment account.

  Specifying a searchAccount has no effect to the account used
  for metrics within the expression (passed via usingMetrics).

- `searchRegion`: string — Region to evaluate search expressions within.

  **Default:** Deployment region.

  Specifying a searchRegion has no effect to the region used
  for metrics within the expression (passed via usingMetrics).

- `stdDevs`: number = 2 — The number of standard deviations to use for the anomaly detection band.

  The higher the value, the wider the band.

  - Must be greater than 0. A value of 0 or negative values would not make sense in the context of calculating standard deviations.
  - There is no strict maximum value defined, as standard deviations can theoretically extend infinitely. However, in practice, values beyond 5 or 6 standard deviations are rarely used, as they would result in an extremely wide anomaly detection band, potentially missing significant anomalies.

- `visible`: boolean = true — Whether this metric should be visible in dashboard graphs.

  Setting this to false is useful when you want to hide raw metrics
  that are used in math expressions, and show only the expression results.
