[英]AWS SNS, sending “continuous” notifications to slack
在高层次上,我写了一个 lambda,它在有错误或没有错误时通知 slack。
从 aws 工具链的角度来看,技术设计如下所示:
验收标准(BDD 风格)
Scenario: As an engineer I want to get notified if my lambda PASSED or FAILED whenever it executes
Given I have a lambda function that runs on a schedule (9am everyday)
Given I have a metric filter that looks for the string "error" in the logs
And I created an alarm that does the following:
# +------------------------+--------------+
# | ALARM |
# +------------------------+--------------+
# | Statistic | Sum |
# | Period | 5 minutes |
# | Threshold type | Static |
# | Alarm condition | >= threshold |
# | Threshold value | 1 |
# | Datapoints to Alarm | 1 of 1 |
# | missing data treatment | ignore |
# | Alarm State | in Alarm |
# +------------------------+--------------+
And I created another alarm that does the following:
# +------------------------+--------------+
# | OK |
# +------------------------+--------------+
# | Statistic | Sum |
# | Period | 5 minutes |
# | Threshold type | Static |
# | Alarm condition | <= threshold |
# | Threshold value | 1 |
# | Datapoints to Alarm | 1 of 1 |
# | missing data treatment | good |
# | Alarm State | OK |
# +------------------------+--------------+
Then EVERY TIME time my function executes without "error" Then I should get "OK"
Then EVERY TIME time my function executes with "error" then I should get "ALARM"
实际行为是它只会发送一次通知,并且只会在警报类型更改时再次发送,即
ALARM -> OK
OK -> ALARM
我似乎没有收到有关此模式的通知
ALARM -> ALRM
OK -> OK
理想情况下,我希望每次 function 执行时都会收到通知
无需使用 CloudWatch 警报。 如果每次 Lambda 执行时都需要一条消息,您应该将 SNS 消息作为 Lambda function 中的最后一件事发布。
try {
// existing code goes here...
snsClient.publish("my-chatbot-topic", "Some success message");
} catch (Exception e) {
snsClient.publish("my-chatbot-topic", "Some error message");
// rethrow the exception so that the lambda still fails for this
throw e;
}
根据AWS 文档:
警报仅针对持续的 state 更改调用操作。 CloudWatch 警报不会仅仅因为它们位于特定的 state 中而调用操作,state 必须已更改并保持指定数量的周期。
一种解决方案是将 stream CW 记录到发送 SNS 消息的 lambda function 中。 通过快速搜索,我发现了这个代码(我自己没有尝试过): https://github.com/codemonauts/aws-cloudwatch-stream-filter-sns-gateway
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.