向 Microsoft Teams 发送 Cloudwatch 警报通知

发布于 2025-01-10 06:55:39 字数 1362 浏览 1 评论 0原文

我正在尝试创建一个自定义 lambda 函数来过滤来自 Cloudwatch 警报的消息并将其传递给 Microsoft Teams。我在 Teams 中创建了一个 Webhook,还创建了 sns 通知并订阅了 lamnda 功能。 我正在使用 Python 创建该自定义函数。下面是我正在使用的代码,但仍然没有运气!

import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
url = "<webhook url>"
msg_obj = json.loads(event['Records'][0]['Sns']['Message'])

msg_str = """
AlarmName: {alarm_name}
""".format(
alarm_name = msg_obj['AlarmName']
)
msg = {
"text": msg_str
}
encoded_msg = json.dumps(msg).encode('utf-8')
resp = http.request('POST',url, body=encoded_msg)
print({
    "message": msg_obj,
    "status_code": resp.status,
    "response": resp.data
})

当我从 SNS 推送测试消息时,出现以下错误

[ERROR] JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 7, in lambda_handler
msg_obj = json.loads(event['Records'][0]['Sns']['Message'])
File "/var/lang/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/var/lang/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/var/lang/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None

** 另请注意,我还不是 Python 大师!任何帮助将不胜感激。

摩西

I am trying to create a custom lambda function to filter the message that comes from Cloudwatch alarm and pass it to Microsoft Teams. I created a webhook in teams and also created the sns notification and subscribed to lamnda function.
I am working with Python to create that custom function. below is the code that I am playing around with but still no luck!

import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
url = "<webhook url>"
msg_obj = json.loads(event['Records'][0]['Sns']['Message'])

msg_str = """
AlarmName: {alarm_name}
""".format(
alarm_name = msg_obj['AlarmName']
)
msg = {
"text": msg_str
}
encoded_msg = json.dumps(msg).encode('utf-8')
resp = http.request('POST',url, body=encoded_msg)
print({
    "message": msg_obj,
    "status_code": resp.status,
    "response": resp.data
})

I get the below error when I push a test message from SNS

[ERROR] JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 7, in lambda_handler
msg_obj = json.loads(event['Records'][0]['Sns']['Message'])
File "/var/lang/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/var/lang/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/var/lang/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None

** Please also note I am not a python guru yet! any help would be greatly appreciated.

Moses

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文