简体   繁体   English

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

[英]AWS SNS, sending “continuous” notifications to slack

On a high level I wrote a lambda that notifies slack when there's an error or not.在高层次上,我写了一个 lambda,它在有错误或没有错误时通知 slack。

From an aws tool-chain perspective, the tech design looks like this:从 aws 工具链的角度来看,技术设计如下所示:

在此处输入图像描述

Acceptance Criteria (in BDD style)验收标准(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"

The actual behavior is it will send out a notification only ONCE, and it will only send again when the alarm type changes ie实际行为是它只会发送一次通知,并且只会在警报类型更改时再次发送,即

  ALARM -> OK
  OK -> ALARM

I don't seem to get notifications for this pattern我似乎没有收到有关此模式的通知

  ALARM -> ALRM
  OK -> OK

Ideally I want to receive a notification every time function executes理想情况下,我希望每次 function 执行时都会收到通知

There's no need to use a CloudWatch alarm.无需使用 CloudWatch 警报。 If you want one message every time the Lambda executes, you should just publish the SNS message as the last thing inside your Lambda function.如果每次 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;
}

As per AWS documentations :根据AWS 文档

Alarms invoke actions for sustained state changes only.警报仅针对持续的 state 更改调用操作。 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. CloudWatch 警报不会仅仅因为它们位于特定的 state 中而调用操作,state 必须已更改并保持指定数量的周期。

One solution is to stream the CW logs to a lambda function that sends the SNS messages.一种解决方案是将 stream CW 记录到发送 SNS 消息的 lambda function 中。 With a fast search I found this code that does exactly this (I didn't try myself): https://github.com/codemonauts/aws-cloudwatch-stream-filter-sns-gateway通过快速搜索,我发现了这个代码(我自己没有尝试过): https://github.com/codemonauts/aws-cloudwatch-stream-filter-sns-gateway

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM