How to define a CloudWatch Alarm on the sum of two metrics with CloudFormation?

好久不见. 提交于 2020-02-02 16:26:27

问题


I need to trigger an alarm when the sum of the same metric (ApproximateNumberOfMessagesVisible) on two different queues exceed the value of 100

In September '17, this answer stated that the only way to do it was with a Lambda function getting the two values and summing them up via CloudWatch API.

At writing time, Feb. '19, it is possible to use "Metric Math", so there is no need to have a lambda function or an EC2 instance. Is it possible to use Metric Math to define an Alarm directly in CloudFormation ?


回答1:


It is actually possible to implement the Alarm logic directly in CloudFormation.

Assuming to have two Scaling Policies ECSScaleUp and ECSScaleDown, the alarm definition will look like:

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


来源:https://stackoverflow.com/questions/54908048/how-to-define-a-cloudwatch-alarm-on-the-sum-of-two-metrics-with-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!