AWS SNS,向 slack 发送 "continuous" 条通知

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

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

无需使用 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;
}

根据AWS documentations

Alarms invoke actions for sustained state changes only. CloudWatch alarms don't invoke actions simply because they are in a particular state, the state must have changed and been maintained for a specified number of periods.

一种解决方案是将 CW 日志流式传输到发送 SNS 消息的 lambda 函数。 通过快速搜索,我发现这段代码正是这样做的(我自己没试过):https://github.com/codemonauts/aws-cloudwatch-stream-filter-sns-gateway