5

I'm having an issue getting a node module to load in AWS Lambda using the Serverless Framework. One of my node packages uses native libraries so I used an EC2 to create the node_module folders and then copied them to my Serverless project. Everything works if I manually zip the project and upload to AWS Lambda but if I use serverless deploy without an artifact specified, I get an error about the module (specifically: ELF file's phentsize not the expected size regarding a .node file)

I've tried adding excludeDevDependencies: false which makes the deployment larger but still gives me the error. Currently, it only works if I zip the contents of the project folder and specify that file as the artifact to upload. Is there a different way to get a node module with native bindings to deploy with Serverless?

UPDATE: After turning off the exclusion of dev dependencies, packaging using serverless package and examining the expanded zip file serverless creates, I discovered the file sizes of the .o and .a files are different in the packaged version as compared to the original. Is this normal?

enter image description here

3
  • your native libraries are .so? Commented Aug 31, 2017 at 6:43
  • I don’t think the issue is with the libraries themselves but more to do with running serverless deploy as it works if I manually copy them. Commented Aug 31, 2017 at 15:59
  • 1
    I'm seeing this too. Testing with an image file in a node module, serverless package followed by unzipping results in an image that I can't open, whereas zipping and unzipping works fine as expected. Commented Sep 5, 2017 at 21:32

2 Answers 2

2

I ran into this problem and did some digging. It turns out that v1.21.0 of serverless broke packaging of binaries.

https://forum.serverless.com/t/serverless-1-21-0-breaks-sharp-library/2606

https://github.com/serverless/serverless/issues/4182

The recommended fix is to upgrade to v1.21.1.

Sign up to request clarification or add additional context in comments.

1 Comment

Good find. You can update this answer. They just released serverless v1.21.1.
0

Since Lambda runs on a Linux container, you should be running serverless deploy from a Linux machine.

This way, your native modules will be compiled for your target architecture which is Linux.

To check the deployment package that serverless creates, you can use sls package or sls deploy --noDeploy (for older versions), and inspect the .serverless directory that it creates. You'll see a zip file in here, extract its contents, and test the code from there.

If the contents of this zip is not what you expect (not the same as when you manually copy them), then maybe something is wrong with your file structure and/or serverless.yml.

7 Comments

When running serverless deploy does it run npm install again? I was under the assumption it just zipped the files in the directory (as that’s what the documentation states).
Your assumption is correct. serverless deploy just zips the directory.
My guess is that there is a problem with your serverless.yml and your project structure (they don't match up). If you can show your serverless.yml and your project tree structure that would be great.
I can get this to happen with an empty project that just requires the mentioned module. I am working around it right now by zipping the deployment package myself and specifying it as the artifact in serverless.yml. I can include the file structure and serverless.yml file if you still think it will help but it's the product of running serverless create --template aws-nodejs and then going into handler.js that is created and requiring the module.
i'm having the same issue with the fibers module (ELF file's phentsize not the expected size), i'm inside a docker and i can require the module before sls package, but once i do the package, if i unzip it and try to require the same module, it shows me the error. Any help? In my personal case, it start happening once i add serverless-typescript-plugin, before it we were able to use fibers successfully
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.