2

I'm trying to use node-phash in an aws lambda function, but coming-up short.

I've found instructions for the node-canvas library here and on github, but still having some trouble.

Steps I've taken:

  1. on an Amazon Linux ec2 instance, I've cloned the node-phash repo
  2. I set an env var to use an rpath, as the node-canvas walkthrough instructed:

    export LDFLAGS=-Wl,-rpath=/var/task/

  3. I determined the shared deps:

    $ readelf -d pHashBinding.node | grep NEEDED
      0x0000000000000001 (NEEDED)             Shared library:      [libstdc++.so.6]
      0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
      0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
      0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
      0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
    
  4. I then copied and downloaded the listed files to include in my zip for lambda

  5. I ran npm install, which went ostensibly fine
  6. I confirmed that the .node would look at the aws lambda root for its deps:

     $ objdump -p build/Release/pHashBinding.node | grep RPATH
        RPATH                /var/task/
    
  7. I then downloaded the build/Release folder and overwrote my project's node_modules/phash/build/Release with the folder from the Amazon Linux environment

  8. I zipped my project for upload to Lambda, including the 5 *.o files from step 3 into the root of the zip, so they'd be unzipped at /var/task/ in Lambda

Unfortunately, after all that, I still get the same errors I did before:

"errorMessage": "/usr/local/lib64/node-v4.3.x/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /var/task/node_modules/node-phash/build/Release/pHashBinding.node)",
  "errorType": "Error",
  "stackTrace": [
    "Object.Module._extensions..node (module.js:434:18)",
    "Module.load (module.js:343:32)",
    "Function.Module._load (module.js:300:12)",
    "Module.require (module.js:353:17)",
    "require (internal/module.js:12:17)",
    "Object.<anonymous> (/var/task/node_modules/node-phash/lib/phash.js:23:13)",
    "Module._compile (module.js:409:26)",
    "Object.Module._extensions..js (module.js:416:10)",
    "Module.load (module.js:343:32)"
  ]
}

libstdc++.so.6 is in the root of my zip. So, how do I get pHashBinding.node to reference the correct directory when looking for dependencies?

Community
  • 1
  • 1
Brandon
  • 7,736
  • 9
  • 47
  • 72
  • op here--if anyone finds themselves in a similar predicament, I eventually just resorted to using a [python image hashing library](https://github.com/JohannesBuchner/imagehash) for this function. I'm not great at Python, but I was able to get a functional microservice setup in short order (and most of the native dependencies for python are widely avail. as prebuilt on aws linux) – Brandon Jul 30 '17 at 21:44

1 Answers1

1

I know this is an older question but I was struggling with this and managed to find a solution that might work for you as well.

This solution comes from a GitHub discussion here https://github.com/grpc/grpc/issues/6443#issuecomment-270558452

Basically you should use Docker to install your packages when you are ready to deploy to AWS Lambda.

  1. Delete the node_modules folder inside your project.
  2. Open terminal and cd into your project folder.
  3. Run this command docker run --rm -it -v "$PWD":/worker -w /worker node:4.2 npm i --production --silent

Afterwards you should see a node_modules folder in your project, this can now be zipped up and uploaded to AWS Lambda.

This should work for any dependencies that require native code when npm install is run.

Hope this helps!

Stefan Charsley
  • 1,409
  • 12
  • 13