Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use any 3rd party module with AWS Lambdas

I am working on a lambda that makes use of modules (async, request, etc)

Unable to import module 'index': Error 
at Function.Module._resolveFilename (module.js:338:15) 
at Function.Module._load (module.js:280:25) 
at Module.require (module.js:364:17) 
at require (module.js:380:17) 
at Object.<anonymous> (/var/task/index.js:1:63) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:312:12) 
at Module.require (module.js:364:17)

Sample code:

var 
  AWS = require('aws-sdk'),
  util = require('util'),
  request = require('request');

exports.handler = function(event, context) {
   console.log('test');
   context.done();
};

It works fine (prints test) as long as no 3rd party modules (besides aws-sdk) are required. As soon as I just add a line such as:

require('request') // or async, config and so on

It fails with the above error. I have tried calling these modules directly as well by specifying the full path with no luck. It's like its looking at the wrong directory when calling require.

Dumping process.env in the console yields:

PATH: '/usr/local/bin:/usr/bin:/bin',
LAMBDA_TASK_ROOT: '/var/task',
LAMBDA_RUNTIME_DIR: '/var/runtime',
AWS_REGION: 'us-west-2',
AWS_DEFAULT_REGION: 'us-west-2',
AWS_LAMBDA_LOG_GROUP_NAME: '/aws/lambda/Thumbnailer',
AWS_LAMBDA_LOG_STREAM_NAME: '2015/12/10/[$LATEST]3f8ef236195448c88f206634bde6301b',
AWS_LAMBDA_FUNCTION_NAME: 'Thumbnailer',
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '512',
AWS_LAMBDA_FUNCTION_VERSION: '$LATEST',
NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules',

Here's the module I was working off - evidently this used to work at some point but does not for me.

Ideas? I feel that I am missing some configuration unique to lambdas here.

like image 584
cyberwombat Avatar asked Dec 10 '15 17:12

cyberwombat


1 Answers

omg that was painful... Turns out that OSX makes the node_modules folder readable only by the user and AWS cannot read it. Make the node_modules folder and content readable by world and it works. I am not sure if all OSX setups react the same. I am using nvm which may be the culprit.

Update. I used to set all files to 0666 but ran into issues with executables. Here's a little script that will address things properly. It will set all files to 0666 unless executables or a directory in which case 0777. Run this from within the project folder (do be careful of the implications of not doing so!):

Here's a script from a question I posted:

#!/bin/bash
find . \
 '(' -perm -0700 -exec chmod 0777 '{}' + ')' -o \
 '(' -perm -0600 -exec chmod 0666 '{}' + ')'
like image 82
cyberwombat Avatar answered Nov 15 '22 03:11

cyberwombat