3

I've a Lambda function with VPC that connecting to Cassandra.

I think that because of cold-start or other issue it can't connect to the Cassandra at all, the Lambda have timeout of 10s, I want to add timeout for the Cassandra as well, if the first connection not being made I will kill the script and return that there was an issue.

I'm using cassandra-driver for node js: https://github.com/datastax/nodejs-driver/

Conneciton:

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], keyspace: 'keyspace' });

I can't use the timeout of the nodejs and then check the connection because the Lambda won't finish the code until the timeout will finished, even if everything was okay.

Rafael Mor
  • 626
  • 1
  • 7
  • 21

1 Answers1

4

It looks like you have timeout available to you as an optional parameter for the Client object here

It should be a matter of assigning this optional parameter to a value of your preference. You should also look for handling the connection problem in the callback function.

const cassandra = require('cassandra-driver');
/* Documentation: github.com/datastax/nodejs-driver/blob/master/lib/client.js - line 120*/
const client = new cassandra.Client(
{ 
   contactPoints: ['127.0.0.1'],
   keyspace: 'keyspace',
   socketOptions:
   {
      connectTimeout: 2000
   }
});

After creating the client you should be able to specify (in case it doesn't work) a callback on the connect method.

/* Documentation: github.com/datastax/nodejs-driver/blob/master/lib/client.js - line 320 */
client.connect(function (err) {
   if (err) return console.error(err); /* your attempt to connect is terminated here. */
   console.log('Connected to cluster with %d host(s): %j', 
   client.hosts.length, client.hosts.keys());
});

Once you've verified your (err) is present - your connection attempt is basically terminated. It's up to you to re-try / kill / do something else with your AWS lambda.

Mavi Domates
  • 4,262
  • 2
  • 26
  • 45