AWS SNS,向松弛发送“连续”通知

时间:2019-09-29 17:49:29

标签: amazon-web-services aws-lambda amazon-sns amazon-cloudwatch

在较高的级别上,我编写了一个lambda来通知是否有错误的松弛。

从aws工具链的角度来看,技术设计如下所示:

enter image description here

接受标准(采用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

理想情况下,我希望每次函数执行时都会收到通知

2 个答案:

答案 0 :(得分:1)

无需使用CloudWatch警报。如果您每次执行Lambda时都希望收到一条消息,则应该将SNS消息发布为Lambda函数中的最后一件事。

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;
}

答案 1 :(得分:1)

根据AWS documentations

  

警报仅针对持续状态更改调用操作。 CloudWatch   警报不会仅仅因为它们处于特定状态而调用动作   状态,该状态必须已更改并针对指定的状态进行了维护   周期数。

一种解决方案是将CW日志流式传输到发送SNS消息的lambda函数。 通过快速搜索,我找到了完全符合此要求的代码(我没有尝试过):https://github.com/codemonauts/aws-cloudwatch-stream-filter-sns-gateway

相关问题