Skip to content

Commit

Permalink
Fix CloudWatch Slack Alerts Lambda message format
Browse files Browse the repository at this point in the history
* Creates a dict rather than creating a JSON string and loading it
* Checks if the message result is a string, and creates a dict if it is
* Removes the 'return True', which is causing the function to exit early
  • Loading branch information
Stretch96 committed Jan 16, 2024
1 parent 223ebb5 commit f4397e4
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lambdas/cloudwatch-slack-alerts/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def lambda_handler(event, context):
try:
message = json.loads(event['Records'][0]['Sns']['Message'])
except ValueError as e:
message = '{"message": "%s"}' (event['Records'][0]['Sns']['Message'].replace('"', '\\"'))
return True
message = { "message": event['Records'][0]['Sns']['Message'] }

if isinstance(message, str):
message = { "message": event['Records'][0]['Sns']['Message'] }

logger.info("Message: " + str(message))

if "AlarmName" in message.keys():
Expand Down

0 comments on commit f4397e4

Please sign in to comment.