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

时间:2019-02-27 14:46:59

标签: amazon-cloudformation amazon-cloudwatch amazon-cloudwatch-metrics

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

'17年9月,this回答说,做到这一点的唯一方法是使用Lambda函数获取两个值并通过CloudWatch API对其求和。

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

1 个答案:

答案 0 :(得分:0)

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

假设有两个扩展策略ECSScaleUpECSScaleDown,警报定义将类似于:

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