You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CloudFormation用Metric Math定义两队列指标求和的CloudWatch告警?

Can I Use Metric Math in CloudFormation to Trigger an Alarm When the Sum of Two SQS Queue Metrics Exceeds 100?

Absolutely yes! You can define this alarm directly in CloudFormation using Metric Math—no Lambda or EC2 instances needed, taking full advantage of the CloudWatch features rolled out in February 2019. This replaces the older Lambda-based approach you mentioned from 2017, making the setup far simpler and more maintainable.

Here's a CloudFormation YAML Example

This template creates a CloudWatch alarm that triggers when the sum of ApproximateNumberOfMessagesVisible across two different SQS queues exceeds 100:

Resources:
  QueueMessageSumAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmName: "SQS-Queue-Message-Sum-Exceeds-100"
      AlarmDescription: "Triggers when total visible messages across two queues exceed 100"
      ComparisonOperator: GreaterThanThreshold
      Threshold: 100
      EvaluationPeriods: 1
      DatapointsToAlarm: 1
      MetricDataQueries:
        # Query 1: Get visible messages from first queue
        - Id: "q1"
          MetricStat:
            Metric:
              Namespace: "AWS/SQS"
              MetricName: "ApproximateNumberOfMessagesVisible"
              Dimensions:
                - Name: QueueName
                  Value: !Ref FirstQueueName  # Replace with your first queue name
            Period: 60
            Stat: Sum
          ReturnData: false
        # Query 2: Get visible messages from second queue
        - Id: "q2"
          MetricStat:
            Metric:
              Namespace: "AWS/SQS"
              MetricName: "ApproximateNumberOfMessagesVisible"
              Dimensions:
                - Name: QueueName
                  Value: !Ref SecondQueueName  # Replace with your second queue name
            Period: 60
            Stat: Sum
          ReturnData: false
        # Query 3: Calculate sum of q1 and q2
        - Id: "sum_query"
          Expression: "SUM(q1, q2)"
          ReturnData: true
      AlarmMetricQuery:
        Id: "sum_query"

Key Details Explained

  • MetricDataQueries: This section defines the three queries needed:
    • q1 and q2: Fetch the ApproximateNumberOfMessagesVisible metric for each of your SQS queues. We set ReturnData: false because we don't need these individual metrics for the alarm itself—only their sum.
    • sum_query: Uses the SUM() expression to add the results of q1 and q2. Setting ReturnData: true tells CloudWatch this is the metric the alarm should evaluate.
  • AlarmMetricQuery: Points to the sum_query ID, so the alarm uses the summed value for its threshold check.
  • Threshold & ComparisonOperator: Set to trigger when the summed value goes above 100.

This approach is clean, serverless, and eliminates the overhead of managing a Lambda function just to calculate a metric sum.

内容的提问来源于stack exchange,提问作者Ing. Luca Stucchi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:59:32