向 Microsoft Teams 发送 Cloudwatch 警报通知
我正在尝试创建一个自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论