0

Is there a way to connect to an RDS instance using the aws-secretsmanager-jdbc with node.js? I need to connect to a datasource and run a simple query, but unfortunately the only supported way to connect is using aws-secretsmanager-jdbc.

https://github.com/aws/aws-secretsmanager-jdbc

I was thinking I could use this node module to connect: https://www.npmjs.com/package/jdbc, like this:

var JDBC = require('jdbc');
var jinst = require('jdbc/lib/jinst');

if (!jinst.isJvmCreated()) {
  jinst.addOption("-Xrs");
  jinst.setupClasspath(['./drivers/hsqldb.jar',
                        './drivers/derby.jar',
                        './drivers/derbyclient.jar',
                        './drivers/derbytools.jar']);
}

var config = {
  url: 'jdbc-secretsmanager:postgresql://therdshost:1234/mydbname',

  // Not sure what to use for drivername here
  drivername: '',
  minpoolsize: 10,
  maxpoolsize: 100,

  user: 'myusername',
  password: '',
  properties: {}
};


var hsqldb = new JDBC(config);

hsqldb.initialize(function(err) {
  if (err) {
    console.log(err);
  }
});

Looking at jdbc-secretsmanager, it seems that this is a separate Java library. Is it possible to do this in node.js or must I use Java for this?

1 Answers1

0

Why don't you use the node.js package to connect to postgres ? checkout https://www.npmjs.com/package/pg and sample code below to get you started with.

You can use aws-sdk for secretsmanager https://www.npmjs.com/package/aws-sdk and use that to get the password and other details to populate the db const below. Here is an example for how to get that. https://gist.github.com/rxgx/7e1b24de5936ff1b2b815a3d9cc3897a

const { Pool, Client } = require('pg');

const db = {
    user: "postgres-user",
    host: "postgres-instance",
    database: "postgres-database",
    password: "postgres-password",
    port: 5432, // default port for RDS postgres 
};

console.log('Starting connection...')

const pool = new Pool(db);
pool.query("SELECT NOW()"), (err, res) => {
   if (err) {
       console.log('pool error', err)
   } 
   console.log('pool => ', p)
   pool.end();
}

const client = new Client(db);
client.connect();
client.query("SELECT NOW()"), (err, res) => { 
   if (err) {
       console.log('client error', err)
   }
   console.log('client => ', res)
   client.end();
}