Amazon CloudWatch Metrics Insights のアラームを利用して、個別に通知は可能か教えてください

2023.11.10

困っていること

多くの EC2 インスタンスの監視を実施しています。
Amazon CloudWatch Metrics Insights のアラーム を利用して StatusCheckFailed が発生(値1)したら、個別に通知することは可能でしょうか教えてください。

どう対応すればいいの?

結論から申し上げますと、GROUP BY 句を使用したクエリ結果を CloudWatch アラームで監視することは叶いません。
Metrics Insights クエリにアラームを設定するにはクエリが 1 つの時系列を返す必要があるためです。

重要
Metrics Insights クエリにアラームを設定するには、クエリが 1 つの時系列を返す必要があります。GROUP BY ステートメントが含まれる場合、GROUP BY ステートメントは、式の最終結果として 1 つの時系列のみを返す Metric Math 式の中にある必要があります。

たとえば、10 個の EC2 インスタンスから StatusCheckFailed メトリクスを収集している場合にクエリを実行すると、GROUP BY 句があるため 10 個(インスタンスの数)のメトリクスが返ります。
このため上記クエリが返すメトリクスを CloudWatch アラームで監視いただくことは叶いません。

一方、GROUP BY 句を削除した場合、10 個の中から例えば平均値(Average)のメトリクスが 1 つだけ返るため監視することは可能です。 しかしながら、こちらは実質 1 つのインスタンスを監視することになるため、ご要件は満たせない方法となります。

確認してみた

弊社ブログ を参考に、順番にアラームが発生するよう各インスタンスへ対して操作を行いました。
結果、SNS でのメール通知や、対象 CloudWatch アラーム (Amazon CloudWatch Metrics Insights アラーム) の履歴においても、個別どのインスタンスから発報されたアラームであるかの確認は叶いません。
また、CloudWatch アラーム履歴に追加されることもございません。

通知例

一部マスクと削除をしています。

CloudWatch アラーム 履歴

履歴データ
2023-11-07 06:07:36でのアラームの状態変化

概要
OKからアラーム状態に更新したアラーム。
データ
{
  "alarmName": "Test1234",
  "alarmType": "MetricAlarm",
  "timestamp": "2023-11-07T06:07:36.842Z",
  "historyItemType": "StateUpdate",
  "historySummary": "Alarm updated from OK to ALARM",
  "historyData": {
    "version": "1.0",
    "oldState": {
      "stateValue": "OK",
      "stateReason": "Threshold Crossed: 1 out of the last 2 datapoints [0.0 (07/11/23 05:47:00)] was not greater than or equal to the threshold (1.0) and 1 missing datapoint was treated as [NonBreaching] (minimum 1 datapoint for ALARM -> OK transition).",
      "stateReasonData": {
        "version": "1.0",
        "queryDate": "2023-11-07T05:52:36.835+0000",
        "startDate": "2023-11-07T05:47:00.000+0000",
        "period": 300,
        "recentDatapoints": [
          0
        ],
        "threshold": 1,
        "evaluatedDatapoints": [
          {
            "timestamp": "2023-11-07T05:47:00.000+0000",
            "value": 0
          }
        ]
      }
    },
    "newState": {
      "stateValue": "ALARM",
      "stateReason": "Threshold Crossed: 2 out of the last 2 datapoints [9.0 (07/11/23 06:02:00), 1.0 (07/11/23 05:57:00)] were greater than or equal to the threshold (1.0) (minimum 2 datapoints for OK -> ALARM transition).",
      "stateReasonData": {
        "version": "1.0",
        "queryDate": "2023-11-07T06:07:36.840+0000",
        "startDate": "2023-11-07T05:57:00.000+0000",
        "period": 300,
        "recentDatapoints": [
          1,
          9
        ],
        "threshold": 1,
        "evaluatedDatapoints": [
          {
            "timestamp": "2023-11-07T06:02:00.000+0000",
            "value": 9
          },
          {
            "timestamp": "2023-11-07T05:57:00.000+0000",
            "value": 1
          }
        ]
      }
    }
  }
}

SNS 通知例  

You are receiving this email because your Amazon CloudWatch Alarm "Test1234" in the Asia Pacific (Tokyo) region has entered the ALARM state, because "Threshold Crossed: 2 out of the last 2 datapoints [9.0 (07/11/23 06:02:00), 1.0 (07/11/23 05:57:00)] were greater than or equal to the threshold (1.0) (minimum 2 datapoints for OK -> ALARM transition)." at "Tuesday 07 November, 2023 06:07:36 UTC".

Alarm Details:
- Name:                       Test1234
- Description:               
- State Change:               OK -> ALARM
- Reason for State Change:    Threshold Crossed: 2 out of the last 2 datapoints [9.0 (07/11/23 06:02:00), 1.0 (07/11/23 05:57:00)] were greater than or equal to the threshold (1.0) (minimum 2 datapoints for OK -> ALARM transition).
- Timestamp:                  Tuesday 07 November, 2023 06:07:36 UTC
- AWS Account:                123456789012
- Alarm Arn:                  arn:aws:cloudwatch:ap-northeast-1:123456789012:alarm:Test1234

Threshold:
- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for at least 2 of the last 2 period(s) of 300 seconds.

Monitored Metrics:
- MetricExpression:           SELECT SUM(StatusCheckFailed) FROM SCHEMA("AWS/EC2", InstanceId)
- MetricLabel:                Test-1234

State Change Actions:
- OK:
- ALARM: [arn:aws:sns:ap-northeast-1:123456789012:CloudWatch-Test]
- INSUFFICIENT_DATA:

参考資料