当两个不同队列上的同一指标(ApproximateNumberOfMessagesVisible
)的总和超过100时,我需要触发警报
'17年9月,this回答说,做到这一点的唯一方法是使用Lambda函数获取两个值并通过CloudWatch API对其求和。
在撰写本文时(19年2月),可以使用“ Metric Math”,因此不需要lambda函数或EC2实例。是否可以使用公制数学直接在CloudFormation中定义警报?
答案 0 :(得分:0)
实际上可以直接在CloudFormation中实现Alarm逻辑。
假设有两个扩展策略ECSScaleUp
和ECSScaleDown
,警报定义将类似于:
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