1

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

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Moses
  • 19
  • 1
  • 1
    What is the contents of `event['Records'][0]['Sns']['Message']`? You can print it in your code and it will appear in CloudWatch Logs to help you debug what is happening. – John Rotenstein Feb 26 '22 at 05:10
  • In Python, whitespace is syntax. Please fix your code's formatting in your question. – Mark B Feb 26 '22 at 14:07

0 Answers0