如何使用CloudFormation定义两个指标之和的CloudWatch警报? [英] How to define a CloudWatch Alarm on the sum of two metrics with CloudFormation?
问题描述
当两个不同队列上的同一指标( roximateNumberOfMessagesVisible
)的总和超过100时,我需要触发警报。
I need to trigger an alarm when the sum of the same metric (ApproximateNumberOfMessagesVisible
) on two different queues exceed the value of 100
17年9月,此答案唯一的方法就是使用Lambda函数获取两个值并通过CloudWatch API对其求和。
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.
在写作时间(19年2月),可以使用 指标数学,因此无需使用lambda函数或EC2实例。是否可以使用Metric Math在CloudFormation中直接定义警报?
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 ?
推荐答案
实际上可以直接实现警报逻辑
It is actually possible to implement the Alarm logic directly in CloudFormation.
假设有两个扩展策略 ECSScaleUp
和 ECSScaleDown
,警报定义如下:
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
这篇关于如何使用CloudFormation定义两个指标之和的CloudWatch警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!