# SearchExpression

A CloudWatch search expression for dynamically finding and graphing multiple related metrics.

Search expressions allow you to search for and graph multiple related metrics from a single expression.
This is particularly useful in dynamic environments where the exact metric names or dimensions
may not be known at deployment time.

Example:
```ts
const searchExpression = new cloudwatch.SearchExpression({
  expression: "SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)",
  label: 'EC2 CPU Utilization',
  period: Duration.minutes(5),
});
```

This class does not represent a resource, so hence is not a construct. Instead,
SearchExpression is an abstraction that makes it easy to specify metrics for use in graphs.

Implements: [IMetric](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.IMetric.html)
Returned by: SearchExpression.with

- 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#L1028)
- Type last changed: v2.276.0 · 10 Nov 2026

## Example

TypeScript:

```ts
const searchExpression = new cloudwatch.SearchExpression({
  expression: "SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)",
  label: 'EC2 CPU Utilization',
  period: Duration.minutes(5),
});
```

Python:

```python
search_expression = cloudwatch.SearchExpression(
    expression="SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)",
    label="EC2 CPU Utilization",
    period=Duration.minutes(5)
)
```

Java:

```java
SearchExpression searchExpression = SearchExpression.Builder.create()
        .expression("SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)")
        .label("EC2 CPU Utilization")
        .period(Duration.minutes(5))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var searchExpression = new SearchExpression(new SearchExpressionProps {
    Expression = "SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)",
    Label = "EC2 CPU Utilization",
    Period = Duration.Minutes(5)
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
searchExpression := cloudwatch.NewSearchExpression(&SearchExpressionProps{
	Expression: jsii.String("SEARCH('{AWS/EC2,InstanceId} CPUUtilization', 'Average', 300)"),
	Label: jsii.String("EC2 CPU Utilization"),
	Period: awscdk.Duration_Minutes(jsii.Number(5)),
})
```

## Initializer

TypeScript:

```ts
new SearchExpression(props: SearchExpressionProps)
```

Python:

```python
SearchExpression(props: SearchExpressionProps)
```

Java:

```java
new SearchExpression(SearchExpressionProps props)
```

C# / .NET:

```csharp
new SearchExpression(SearchExpressionProps props)
```

Golang:

```go
NewSearchExpression(Props SearchExpressionProps)
```

## Properties

- `color`: string — Hex color code (e.g. '#00ff00'), to use when rendering the resulting metrics in a graph. If multiple time series are returned, color is assigned to the first metric, color for the other metrics is automatically assigned.

- `expression`: string — The search expression defining the metrics to be retrieved.

- `label`: string — The label is used as a prefix for the title of each metric returned by the search expression.

- `period`: [Duration](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Duration.html) — The aggregation period for the metrics produced by the Search Expression.

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

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

- `visible`: boolean — Whether the time series produced by this search expression should be visible in dashboard graphs.

- `warnings`: string[] — - use warningsV2

- `warningsV2`: { [key: string]: string } — Warnings generated by this search expression.

## Methods

- `toMetricConfig(): MetricConfig` — Inspect the details of the metric object.

- `toString(): string` — Returns a string representation of an object.

- `with(props: SearchExpressionOptions): SearchExpression // chainable` — Return a copy of SearchExpression with properties changed.

  All properties except expression can be changed.

  **Parameters**

  - `props` — The set of properties to change.
