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 250 forks source link

TypeError: Cannot read property '0' of undefined #18

Closed rohitrsh closed 7 years ago

rohitrsh commented 7 years ago

Hi,

First off thanks for putting it all together.

I wanted to use this stack only for CloudWatch alerts and AutoScale Notification. I am after deploying it on my AWS account, I am successfully able to receive message from CloudWatch. But not getting events of AutoScale.

Just to troubleshoot i have tried to run make test. To do so i have removed all json files from test folder except context.json sns-autoscaling-event.json I am getting below error message. Though same test is working while i am putting all test json files in test folder

/opt/lambda-cloudwatch-slack/test/sns-cloudwatch-event.json file successfully created
sns received:{
  "key": "value",
  "key2": "value2",
  "other_key": "other_value"
}
/opt/lambda-cloudwatch-slack/index.js:268
  var eventSubscriptionArn = event.Records[0].EventSubscriptionArn;
                                          ^

TypeError: Cannot read property '0' of undefined
    at processEvent (/opt/lambda-cloudwatch-slack/index.js:268:43)
    at exports.handler (/opt/lambda-cloudwatch-slack/index.js:316:5)
    at Lambda._runHandler (/opt/lambda-cloudwatch-slack/node_modules/node-lambda/lib/main.js:97:7)
    at Lambda.run (/opt/lambda-cloudwatch-slack/node_modules/node-lambda/lib/main.js:54:8)
    at Command.<anonymous> (/opt/lambda-cloudwatch-slack/node_modules/node-lambda/bin/node-lambda:99:12)
    at Command.listener (/opt/lambda-cloudwatch-slack/node_modules/commander/index.js:301:8)
    at emitTwo (events.js:106:13)
    at Command.emit (events.js:191:7)
    at Command.parseArgs (/opt/lambda-cloudwatch-slack/node_modules/commander/index.js:615:12)
    at Command.parse (/opt/lambda-cloudwatch-slack/node_modules/commander/index.js:458:21)
make: *** [test] Error 1

Could you please help me know to know what i am missing here?

-Rohit

creichert commented 7 years ago

@rohitrsh I just saw this and it's a little late here. I'll be able to get look into this more tomorrow.

If possible, try to use console.log to print the event before the var eventSubscriptionArn statement and post the sanitized output here.

CodyReichert commented 7 years ago

@rohitrsh - Where did you get this event?

{
  "key": "value",
  "key2": "value2",
  "other_key": "other_value"
}

In the output you posted, that event is being logged right here, and two lines down is where the error is being thrown, here. A proper AutoScaling event should have a resources array in it, like the examples in the AWS docs here.

It sounds like this error is only being thrown with your test event that you posted. Is that correct?

CodyReichert commented 7 years ago

Hey @rohitrsh - I'm going to close this because it looks like an issue with the test even you used, but if you're still working on this and having troubles please do re-open and I'll be happy to take a look!

ksac1 commented 5 years ago

I am getting a request through : const request = event.Records[0].cf.request;

And this is giving me error: Cannot read property '0' of undefined at exports.handler.