Serverless, node and Hummus. Compiling a binary does not work

1.5k Views Asked by At

I'm trying to use HummusJS on a Lambda function using the Serverless framework for deployment.

The Hummus npm library needs a binary compiled to function. This (hummus.node) is provided in its node_modules folder. However when I require 'hummus' the library is throwing a bug.

const hummus = require('hummus');

module.exports.stamp = (event, context, callback) => {
    const response = {
        statusCode: 200,
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': true,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ url: 'url' })
    };
    callback(null, response);
};

module initialization error: Error at Error (native) at Object.Module._extensions..node (module.js:597:18) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (/var/task/pdf-manipulator/node_modules/hummus/hummus.js:5:31) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10)

2

There are 2 best solutions below

0
On BEST ANSWER

I used docker-lambda to build the binary and then copied it over to '/node_modules/hummus/bindings/hummus.node'.

https://github.com/lambci/docker-lambda

I could then run the app in docker-lambda which reproduced the lambda environment.

0
On

The docker-lambda solution definitely works. However, a colleague pointed out an even easier solution that doesn't require docker-lambda.

hummus uses node-pre-gyp to build the hummus.node binary, and there is an environmental variable EXTRA_NODE_PRE_GYP_FLAGS to supply additional flags. If the proper flags are configured for AWS Lambda's runtime environment, node-pre-gyp will actually find the right pre-built binary that exists at https://hummus.s3-us-west-2.amazonaws.com/.

To do this, set the environment variable by doing:

export EXTRA_NODE_PRE_GYP_FLAGS='--target_arch=x64 --target_platform=linux --target_libc=glibc'

then do npm install and it should pull in the correct binary for AWS Lambda.