ibmdb / node-ibm_db

IBM DB2 and IBM Informix bindings for node
MIT License
188 stars 151 forks source link

Not able to run or connect ibm db2 from AWS lambda #957

Closed omkarsunku closed 8 months ago

omkarsunku commented 8 months ago

I am trying the below code in AWS lambda.

const ibmdb = require('ibm_db');

exports.handler = async (event) => {
    // Extract the required input parameters from the Lambda event or context
    const { database, hostname, uid, pwd, port, security, protocol, tableName } = event;

    // Construct the connection string based on the provided parameters
    const connString = `DATABASE=${database};HOSTNAME=${hostname};UID=${uid};PWD=${pwd};PORT=${port};SECURITY=${security};PROTOCOL=${protocol}`;

    // Create a Lambda function that connects to the database, executes a query, and closes the connection
    return new Promise((resolve, reject) => {
        ibmdb.open(connString, (err, conn) => {
            if (err) {
                console.error('Error connecting to the database:', err);
                reject(err);
                return;
            }

            // Database connection is successful, you can now execute SQL queries
            const selectQuery = `SELECT * FROM ${tableName}`; // Use the provided table name in the SQL query
            conn.query(selectQuery, (err, rows) => {
                if (err) {
                    console.error('Error executing query:', err);
                    reject(err);
                } else {
                    console.log('Query result:', rows);
                    resolve(rows);
                }

                // Close the connection
                conn.close((err) => {
                    if (err) {
                        console.error('Error closing the connection:', err);
                    }
                });
            });
        });
    });
};
For the above code i am getting below error 
{
  "errorType": "Error",
  "errorMessage": "/var/task/node_modules/ibm_db/build/Release/odbc_bindings.node: invalid ELF header",
  "trace": [
    "Error: /var/task/node_modules/ibm_db/build/Release/odbc_bindings.node: invalid ELF header",
    "    at Module._extensions..node (node:internal/modules/cjs/loader:1340:18)",
    "    at Module.load (node:internal/modules/cjs/loader:1119:32)",
    "    at Module._load (node:internal/modules/cjs/loader:960:12)",
    "    at Module.require (node:internal/modules/cjs/loader:1143:19)",
    "    at require (node:internal/modules/cjs/helpers:121:18)",
    "    at bindings (/var/task/node_modules/bindings/bindings.js:112:48)",
    "    at Object.<anonymous> (/var/task/node_modules/ibm_db/lib/odbc.js:57:31)",
    "    at Module._compile (node:internal/modules/cjs/loader:1256:14)",
    "    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)",
    "    at Module.load (node:internal/modules/cjs/loader:1119:32)"
  ]
}

package.json dependencies "dependencies": { "ibm_db": "^3.2.2" }

Please suggest what could be the issue and solution for this.

Thanks

bimalkjha commented 8 months ago

@omkarsunku You have not provided any info as requested in the new issue template. Without complete info, it is difficult to help. Please provide complete output of below commands:

 uname
  uname -m
  node -v
  npm ls ibm_db
  db2level
npm install ibm_db

By looking on the error output - odbc_bindings.node: invalid ELF header, it tells that the odbc_bindings.node file is not compatible with the architecture of this system. It seems you have installed ibm_db somewhere else and then copied these file which is incorrect way of using ibm_db. You need to install locally. Now, you can run below commands and share the complete output, not partial:

cd /var/task/node_modules/ibm_db/
npm install

If install is success, then you can try to run your test program. Thanks.

omkarsunku commented 8 months ago

Hello @bimalkjha,

Thanks for the response.

uname uname -m node -v npm ls ibm_db db2level Linux x86_64 v12.22.9 lambda-connectdb2@1.0.0 /home/ubuntu/lambda-connectDB2 └── ibm_db@3.2.2

db2level: command not found

Ran npm install and below is the complete logs npm install npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'fs-extra@11.1.1', npm WARN EBADENGINE required: { node: '>=14.14' }, npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' } npm WARN EBADENGINE }

ibm_db@3.2.2 install node installer/driverInstall.js

platform = linux, arch = x64, node.js version = v12.22.9 make version =GNU Make 4.3 Rebuild Process: Found clidriver at -> /home/ubuntu/lambda-connectDB2/node_modules/ibm_db/installer/clidriver

Downloading of clidriver skipped - build is in progress...

make: Entering directory '/home/ubuntu/lambda-connectDB2/node_modules/ibm_db/build' CXX(target) Release/obj.target/odbc_bindings/src/odbc.o CXX(target) Release/obj.target/odbc_bindings/src/odbc_connection.o CXX(target) Release/obj.target/odbc_bindings/src/odbc_statement.o CXX(target) Release/obj.target/odbc_bindings/src/odbc_result.o SOLINK_MODULE(target) Release/obj.target/odbc_bindings.node COPY Release/odbc_bindings.node make: Leaving directory '/home/ubuntu/lambda-connectDB2/node_modules/ibm_db/build'

ibm_db installed successfully.

added 50 packages, and audited 51 packages in 17s

3 packages are looking for funding run npm fund for details

found 0 vulnerabilities

Then i tried to run my lambda it gave same error { "errorType": "Error", "errorMessage": "/var/task/node_modules/ibm_db/build/Release/odbc_bindings.node: invalid ELF header", "trace": [ "Error: /var/task/node_modules/ibm_db/build/Release/odbc_bindings.node: invalid ELF header", " at Module._extensions..node (node:internal/modules/cjs/loader:1340:18)", " at Module.load (node:internal/modules/cjs/loader:1119:32)", " at Module._load (node:internal/modules/cjs/loader:960:12)", " at Module.require (node:internal/modules/cjs/loader:1143:19)", " at require (node:internal/modules/cjs/helpers:119:18)", " at bindings (/var/task/node_modules/ibm_db/node_modules/bindings/bindings.js:112:48)", " at Object. (/var/task/node_modules/ibm_db/lib/odbc.js:57:31)", " at Module._compile (node:internal/modules/cjs/loader:1256:14)", " at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)", " at Module.load (node:internal/modules/cjs/loader:1119:32)" ] }

omkarsunku commented 8 months ago

Also when i run the below code without a lambda format this works (i have obtained a public DB2 from IBM cloud) const ibmdb = require('ibm_db');

// Connection details const connString = 'DATABASE=MY_DB;' + 'HOSTNAME=MY_HOSTNAME' + 'UID=MY_UID;' + 'PWD=MY_PWD;' + 'PORT=32107;' + "SECURITY=SSL;" + 'PROTOCOL=TCPIP';

// Create a connection ibmdb.open(connString, (err, conn) => { if (err) { console.error('Error connecting to the database:', err); return; }

// Database connection is successful, you can now execute SQL queries
conn.query('SELECT * FROM sample_table', (err, rows) => {
    if (err) {
        console.error('Error executing query:', err);
    } else {
        console.log('Query result:', rows);
    }

    // Close the connection
    conn.close((err) => {
        if (err) {
            console.error('Error closing the connection:', err);
        } else {
            console.log('Connection closed.');
        }
    });
});

});

bimalkjha commented 8 months ago

@omkarsunku Your installation of ibm_db is correct. It can be confirmed by running the test file. So, node has no issue in using ibm_db. As per shared output, the location of installed ibm_db is : /home/ubuntu/lambda-connectDB2/node_modules/ibm_db So, when you run test program using node, it picks the file /home/ubuntu/lambda-connectDB2/node_modules/ibm_db/build/Release/odbc_bindings.nodeand it is the correct file which get created post install of ibm_db. But, in your error output, we can see the application is trying to load file from/var/task/node_modules/ibm_db/build/Release/odbc_bindings.node`, which is incorrect file and hence error.

You have installed ibm_db in path /home/ubuntu/lambda-connectDB2/node_modules/ibm_db and not at /var/task/node_modules/ibm_db. You need to run below commands:

cd /var/task/node_modules/ibm_db
npm install
node test/test-basic-test.js

Now you'll get correct file /var/task/node_modules/ibm_db/build/Release/odbc_bindings.node. Please share complete output of above npm install command if it still does not work. Thanks.

bimalkjha commented 8 months ago

Basically, below commands are expected to work:

cd /home/ubuntu/lambda-connectDB2/
npm install ibm_db
npm ls ibm_db
vi node_modules/ibm_db/test/config.json => update database  connection info.
node test/test-basic-test.js

cd /var/task/
npm install ibm_db
npm ls ibm_db
vi node_modules/ibm_db/test/config.json => update database  connection info.
node test/test-basic-test.js

Thanks.

omkarsunku commented 8 months ago

Yes this works on Ubuntu at this path cd /home/ubuntu/lambda-connectDB2/ and other path is the lambda path where i can't open the lambda and try that. Do you have a steps to run a ibm db2 on aws lambda like this looks like a special case where lambda environment is different than which i tried in other path?

bimalkjha commented 8 months ago

@omkarsunku I have no idea about what is lambda. If its an OS, you should be able to login on it and get command prompt and then install. You can getting this issue because lambda OS is definitely different for Ubunto and hence error. Also, it is not amd64 architecture. The ELF error says, you have installed ibm_db on x86_64 architecture and copied it on different architecture, hence error. You must need to install ibm_db locally before use. Installing on one system and copying to another and then use, will not work. Please go through the issue #912 and #616 in detail, which is about installation of ibm_db on lamda only and users were able to use it successfully. Thanks.

omkarsunku commented 8 months ago

@bimalkjha Thanks for you guidance it worked by adding required files on to the layer. Like i have create a layer with all the below files to zip and attached that layer to lambda. Files added to create a layer libaudit.so.1 libcap-ng.so.0 libcrypt.so.1 liblzma.so.5 libpam.so.0 libxml2.so.2

ls-jesusplasencia commented 4 months ago

Hi, @omkarsunku, I am having the same issue, I installed ibm_db, but when I run the lambda it crashed into 502 internal error, could you please share with me what should be the content of those files you mentioned before? For example, in my case, it failed because I dont have libcrypt.so.1, but I wonder, this file should contain something or should I leave it as empty?