

這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護，並於 2023 年 6 月 1 日結束支援。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定 CloudWatch 警示
<a name="how-to-set-cw-alarm"></a>

使用 [https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch-readme.html](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch-readme.html)套件在 Amazon CloudWatch CloudWatch 警示。您可以使用預先定義的指標或建立自己的指標。

## 使用現有的指標
<a name="how-to-set-cw-alarm-use-metric"></a>

許多 AWS 建構程式庫模組可讓您透過將指標名稱傳遞至具有指標之物件執行個體上的便利方法，在現有指標上設定警示。例如，假設有 Amazon SQS 佇列，您可以從佇列的 [https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sqs.Queue.html#metricmetricname-props](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sqs.Queue.html#metricmetricname-props)方法取得指標 **ApproximateNumberOfMessagesVisible**：

**Example**  

```
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
```

```
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
```

```
metric = queue.metric("ApproximateNumberOfMessagesVisible")
```

```
Metric metric = queue.metric("ApproximateNumberOfMessagesVisible");
```

```
var metric = queue.Metric("ApproximateNumberOfMessagesVisible");
```

## 建立您自己的指標
<a name="how-to-set-cw-alarm-new-metric"></a>

建立您自己的[指標](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Metric.html)，如下所示，其中*命名空間*值應該與 Amazon ** SQS 佇列的 AWS/**SQS 類似。 Amazon SQS 您也需要指定指標的名稱和維度：

**Example**  

```
const metric = new cloudwatch.Metric({
  namespace: 'MyNamespace',
  metricName: 'MyMetric',
  dimensionsMap: { MyDimension: 'MyDimensionValue' }
});
```

```
const metric = new cloudwatch.Metric({
  namespace: 'MyNamespace',
  metricName: 'MyMetric',
  dimensionsMap: { MyDimension: 'MyDimensionValue' }
});
```

```
metric = cloudwatch.Metric(
    namespace="MyNamespace",
    metric_name="MyMetric",
    dimensionsMap=dict(MyDimension="MyDimensionValue")
)
```

```
Metric metric = Metric.Builder.create()
        .namespace("MyNamespace")
        .metricName("MyMetric")
        .dimensionsMap(java.util.Map.of(    // Java 9 or later
            "MyDimension", "MyDimensionValue"))
        .build();
```

```
var metric = new Metric(this, "Metric", new MetricProps
{
    Namespace = "MyNamespace",
    MetricName = "MyMetric",
    Dimensions = new Dictionary<string, object>
    {
        { "MyDimension", "MyDimensionValue" }
    }
});
```

## 建立警示
<a name="how-to-set-cw-alarm-create"></a>

一旦擁有您定義的現有指標或指標，您就可以建立警示。在此範例中，當過去三個評估期間中有兩個 100 個以上的指標時，就會發出警示。您可以透過 `comparisonOperator` 屬性在警示中使用小於 等比較。Greater-than-or-equal-to是 AWS CDK 預設值，因此我們不需要指定它。

**Example**  

```
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
  metric: metric,
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});
```

```
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
  metric: metric,
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2
});
```

```
alarm = cloudwatch.Alarm(self, "Alarm",
    metric=metric,
    threshold=100,
    evaluation_periods=3,
    datapoints_to_alarm=2
)
```

```
import software.amazon.awscdk.services.cloudwatch.Alarm;
import software.amazon.awscdk.services.cloudwatch.Metric;

Alarm alarm = Alarm.Builder.create(this, "Alarm")
        .metric(metric)
        .threshold(100)
        .evaluationPeriods(3)
        .datapointsToAlarm(2).build();
```

```
var alarm = new Alarm(this, "Alarm", new AlarmProps
{
    Metric = metric,
    Threshold = 100,
    EvaluationPeriods = 3,
    DatapointsToAlarm = 2
});
```

建立警示的替代方法是使用 指標的 方法，該[https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Metric.html#createwbralarmscope-id-props](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Metric.html#createwbralarmscope-id-props)方法基本上採用與`Alarm`建構函數相同的屬性。您不需要傳入 指標，因為它已知。

**Example**  

```
metric.createAlarm(this, 'Alarm', {
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});
```

```
metric.createAlarm(this, 'Alarm', {
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});
```

```
metric.create_alarm(self, "Alarm",
    threshold=100,
    evaluation_periods=3,
    datapoints_to_alarm=2
)
```

```
metric.createAlarm(this, "Alarm", new CreateAlarmOptions.Builder()
        .threshold(100)
        .evaluationPeriods(3)
        .datapointsToAlarm(2)
        .build());
```

```
metric.CreateAlarm(this, "Alarm", new CreateAlarmOptions
{
    Threshold = 100,
    EvaluationPeriods = 3,
    DatapointsToAlarm = 2
});
```