Having an issue with a lambda function that connects to a remote MongoDB on an EC2 instance using TypeScript. While I can connect to the database locally, there is an ELF error when running in lambda. It seems to be related to mismatched binaries of npm packages being built for different environments (linux/mac/windows).
The exact error message is:
Stack: ERROR:/var/task/node_modules/snappy/build/Release/binding.node:Invalid ELF Header.
Interestingly, this error only occurs with certain npm packages like mongoose or mongoDB native package when they are installed.
The EC2 instance runs Ubuntu server while the lambda runs node. The code is uploaded/packaged to lambda using serverless template.yml. What could be causing this error? Is it lambda itself or something specific to my EC2 OS?
I don't believe the Linux OS on EC2 is the root cause since it's just hosting the DB and should not affect the environment compatibility issue. Please correct me if I'm wrong.
The connection string and relevant code snippet are as follows:
import mongoose from 'mongoose';
mongoose.connect('mongodb://IPADDRESS:<PORT>/DB');
The npm package version is:
"mongoose": "^5.12.12",
If I remove the mongo connection string/packages, everything works fine. This issue persists with both mongodb native and mongoose packages. How would you suggest debugging/solving this problem, especially considering many developers use mongoose with lambda without issues?