如何使用 CloudFormation 在两个指标的总和上定义 CloudWatch 警报?

Luc*_*chi 1 aws-cloudformation amazon-cloudwatch amazon-cloudwatch-metrics

ApproximateNumberOfMessagesVisible两个不同队列上的相同指标 ( )的总和超过 100时,我需要触发警报

在 17 年 9 月,这个答案指出唯一的方法是使用 Lambda 函数获取两个值并通过 CloudWatch API 将它们相加。

在撰写本文时,2019 年 2 月,可以使用“ Metric Math ”,因此不需要 lambda 函数或 EC2 实例。是否可以使用 Metric Math 直接在 CloudFormation 中定义警报?

Luc*_*chi 5

实际上可以直接在 CloudFormation 中实现报警逻辑。

假设有两个 Scaling PoliciesECSScaleUpECSScaleDown,警报定义将如下所示:

ECSWorkerSQSCumulativeAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: !Join ['-', [!Ref 'MyService', 'SQSCumulativeAlarm']]
    AlarmDescription: "Trigger ECS Service Scaling based on TWO SQS queues"
    Metrics:
      - Id: e1
        Expression: "fq + sq"
        Label: "Sum of the two Metrics"
      - Id: fq
        MetricStat:
          Metric:
            MetricName: ApproximateNumberOfMessagesVisible
            Namespace: AWS/SQS
            Dimensions:
              - Name: QueueName
                Value: !GetAtt [ FirstQueue, QueueName]
        Period: 60
        Stat: Average
        Unit: Count
        ReturnData: false
      - Id: sq
        MetricStat:
          Metric:
            MetricName: ApproximateNumberOfMessagesVisible
            Namespace: AWS/SQS
            Dimensions:
              - Name: QueueName
                Value: !GetAtt [ SecondQueue, QueueName]
          Period: 60
          Stat: Average
          Unit: Count
        ReturnData: false
    EvaluationPeriods: 2
    Threshold: 100
    ComparisonOperator: GreaterThanThreshold
    AlarmActions:
      - !Ref ECSScaleUp
      - !Ref ECSScaleDown
    OKActions:
      - !Ref ECSScaleUp
      - !Ref ECSScaleDown
Run Code Online (Sandbox Code Playgroud)