Search code examples
node.jsamazon-web-servicesaws-lambdaaws-sdkweb-worker

Can I use WebWorkers in AWS Lambda (node 6.10)


I've got a very simple node module that I want in AWS lambda, and its importing 'Natural' for some text processing.

This line throws the error: var n = require('natural');

The error is this:

{
  "errorMessage": "/var/task/node_modules/webworker-threads/build/Release/WebWorkerThreads.node: invalid ELF header",
  "errorType": "Error",
  "stackTrace": [
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "bindings (/var/task/node_modules/bindings/bindings.js:76:44)",
    "Object.<anonymous> (/var/task/node_modules/webworker-threads/index.js:1:105)",
    "Module._compile (module.js:570:32)"
  ]
}

and it returns a

{
  "message": "Internal server error"
}

Does AWS lambda not run webworkers? I've tried compiling with nvm 6.10 exactly and rebuilt the zip file, but to no avail. Is there something else I need to include in the zip to make sure the aws lambda env supports the webworker module?

EDIT: (Solved) I wrote up a long tutorial and blog with code to dockerize the Native compilation process.

https://medium.com/@james_mtc/how-to-unsuck-aws-lambdas-native-compilation-for-nodejs-functions-with-docker-and-bash-5301a26a19b1


Solution

  • You need to build the native modules on an Amazon AMI with static rather than shared/dynamic libraries. Changing a module from shared to static might be non trivial for some packages.

    Native modules are similarly installed and deployed, but you’ll need to build them against the Amazon Linux libraries. You’ll need to either ensure that the libraries and their transitive dependencies are statically compiled or use rpath-style linking; we’ll do it the static way in this post, and demonstrate use of rpath in a subsequent post. (Note that many, but not all, libraries can be statically linked this way.)