assertible / lambda-cloudwatch-slack

Send AWS CloudWatch notifications to a Slack channel using Lambda
https://assertible.com/blog/npm-package-lambda-cloudwatch-slack
MIT License
482 stars 249 forks source link

Bug: record.Sns.Message is not necessarily an object #38

Open gabauer opened 5 years ago

gabauer commented 5 years ago

How to produces:

Goto AWS SNS -> Topic -> public a message

The message looks like:

{
    "Records": [
        {
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            "EventSubscriptionArn": "arn:aws:sns:XXXXXXXXXXXXXXX-monitoring:9b9asdff5-be7f-safa-958a-000000000000",
            "Sns": {
                "Type": "Notification",
                "MessageId": "318b60f7-fae6sdfd-5fasd93-ae63-XXXXXXXXXX",
                "TopicArn": "arn:aws:sns:XXXXXXXXXXXXXX-monitoring",
                "Subject": "Test - AWS Cloudwatch - Lamda",
                "Message": "Test - AWS Cloudwatch - Lamda",
                "Timestamp": "2019-03-07T07:00:50.306Z",
                "SignatureVersion": "1",
                "Signature": "Kmdfh5Lr5LpBeTXgZJRdPHohmHRdQn6NmXXXXXXXXXXXXXXXXXXXXXXXXXxthnjHorUS+BNbW9892imFRcH==",
                "SigningCertUrl": "https://sns.XXXXXXXXXXX.pem",
                "UnsubscribeUrl": "https://sns.XXXXXXXXXXX958a-a3sd12jf4l3ka4s8d",
                "MessageAttributes": {}
            }
        }
    ]
}

Records[0].Sns.Message is no object in this case and JSON.parse fails here:

index.js:319  var message = JSON.parse(record.Sns.Message)

How to fix (one way):

Check if Records[0].Sns.Message is a object:

index.js:319 var message = typeof record.Sns.Message === 'object' 
index.js:320                ? JSON.parse(record.Sns.Message)
index.js:321                : { message: record.Sns.Message}