選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

設定 CloudWatch 警示

焦點模式
設定 CloudWatch 警示 - AWS Cloud Development Kit (AWS CDK) v2

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

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

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

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

使用 aws-cloudwatch 套件在 Amazon CloudWatch CloudWatch 警示。您可以使用預先定義的指標或建立自己的指標。

使用現有的指標

許多 AWS 建構程式庫模組可讓您在具有指標的物件執行個體上,將指標名稱傳遞至便利方法,藉此在現有指標上設定警示。例如,指定 Amazon SQS 佇列時,您可以從佇列的 metric() 方法取得指標 ApproximateNumberOfMessagesVisible

TypeScript
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
JavaScript
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
Python
metric = queue.metric("ApproximateNumberOfMessagesVisible")
Java
Metric metric = queue.metric("ApproximateNumberOfMessagesVisible");
C#
var metric = queue.Metric("ApproximateNumberOfMessagesVisible");
const metric = queue.metric("ApproximateNumberOfMessagesVisible");

建立您自己的指標

建立您自己的指標,如下所示,其中命名空間值應該與 Amazon SQS 佇列的 AWS/SQS 類似。 Amazon SQS 您也需要指定指標的名稱和維度:

TypeScript
const metric = new cloudwatch.Metric({ namespace: 'MyNamespace', metricName: 'MyMetric', dimensionsMap: { MyDimension: 'MyDimensionValue' } });
JavaScript
const metric = new cloudwatch.Metric({ namespace: 'MyNamespace', metricName: 'MyMetric', dimensionsMap: { MyDimension: 'MyDimensionValue' } });
Python
metric = cloudwatch.Metric( namespace="MyNamespace", metric_name="MyMetric", dimensionsMap=dict(MyDimension="MyDimensionValue") )
Java
Metric metric = Metric.Builder.create() .namespace("MyNamespace") .metricName("MyMetric") .dimensionsMap(java.util.Map.of( // Java 9 or later "MyDimension", "MyDimensionValue")) .build();
C#
var metric = new Metric(this, "Metric", new MetricProps { Namespace = "MyNamespace", MetricName = "MyMetric", Dimensions = new Dictionary<string, object> { { "MyDimension", "MyDimensionValue" } } });
const metric = new cloudwatch.Metric({ namespace: 'MyNamespace', metricName: 'MyMetric', dimensionsMap: { MyDimension: 'MyDimensionValue' } });

建立警示

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

TypeScript
const alarm = new cloudwatch.Alarm(this, 'Alarm', { metric: metric, threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2, });
JavaScript
const alarm = new cloudwatch.Alarm(this, 'Alarm', { metric: metric, threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2 });
Python
alarm = cloudwatch.Alarm(self, "Alarm", metric=metric, threshold=100, evaluation_periods=3, datapoints_to_alarm=2 )
Java
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();
C#
var alarm = new Alarm(this, "Alarm", new AlarmProps { Metric = metric, Threshold = 100, EvaluationPeriods = 3, DatapointsToAlarm = 2 });
const alarm = new cloudwatch.Alarm(this, 'Alarm', { metric: metric, threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2, });

建立警示的替代方法是使用指標的 createAlarm() Alarm 方法,該方法基本上採用與建構函數相同的屬性。您不需要傳入 指標,因為它已知。

TypeScript
metric.createAlarm(this, 'Alarm', { threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2, });
JavaScript
metric.createAlarm(this, 'Alarm', { threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2, });
Python
metric.create_alarm(self, "Alarm", threshold=100, evaluation_periods=3, datapoints_to_alarm=2 )
Java
metric.createAlarm(this, "Alarm", new CreateAlarmOptions.Builder() .threshold(100) .evaluationPeriods(3) .datapointsToAlarm(2) .build());
C#
metric.CreateAlarm(this, "Alarm", new CreateAlarmOptions { Threshold = 100, EvaluationPeriods = 3, DatapointsToAlarm = 2 });
metric.createAlarm(this, 'Alarm', { threshold: 100, evaluationPeriods: 3, datapointsToAlarm: 2, });
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。