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