Jack Price-Burns
Jack Price-Burns

Reputation: 190

Lambda function failing with Unable to import module 'index'

Error:

Unable to import module 'index': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/node_modules/slack-incoming-webhook/lib/index.js:3:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)

By the looks of this my code isn't the problem it's a problem with the slack-incoming-webhook node-module however the piece of offending code is this line which looks completely normal.

var SlackClient = require('./client');

I have tried 4 different packages now (request, http, node-webhooks and now slack-incoming-webhooks) and they are all failing with code in node modules. I am thoroughly confused as I can get the code to work on my own computer and on an Amazon Linux AMI EC2 Instance (running same node version)

All the code is zipped and sent to lambda using the aws-cli and I have deployed node.js code on lambda before without any problems (alexa skill).

I have tried npm install on the ec2 instance, I have tried several different packages and I have come to the conclusion there must be some sort of configuration wrong in lambda but I can't find what. Could someone point me in the right direction...

Here is my code if anyone is curious also the lambda trigger is an aws iot button.

const slack = require('slack-incoming-webhook');
const send = slack({
    url: 'https://hooks.slack.com/....'
});

exports.handler = function ()
{
    send(process.env.company + ' has pushed their panic button! PANIC! PANIC! PANIC!');
};

Upvotes: 1

Views: 2456

Answers (2)

Kamal
Kamal

Reputation: 2640

I would simply refer to use Apex (http://apex.run/). Pretty much awsm serverless framework to be used with AWS Lambda. Once this is setup, no need to do manual zipping.

Simply execute couple of commands:

  1. apex create (to create the lambda)
  2. apex deploy (deploy to your AWS region, no manual zipping required)
  3. apex invoke to invoke it from your terminal.

Thanks

Upvotes: 0

Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8571

This is common issue I have seen in many posts. Most of the cases it is the way zipping the files making the problem. Instead of zipping the folder you have to select all files and zip it like below,

enter image description here

Upvotes: 4

Related Questions