0

I have an elastic search connection in my code as below.

const config = require('../../config/index');
const logger = require('winston');

var elasticsearch = require('elasticsearch');
var elasticClient;

var state = {
  connection: null,
}

exports.connect = function (done) {
try {
    logger.info("elasticsearch 000");
    if (state.connection) return done()
    elasticClient = new elasticsearch.Client({
      host: config.elasticSearch.url,
      log: 'info'
    });

    state.connection = elasticClient;
    logger.info("elasticsearch connected on url : ", config.elasticSearch.url);
    done()

  } catch (e) {
    logger.info("elasticsearch connect exception ", e)
  }

}

exports.get = function () {
  return state.connection
}

I'm using this connection in this way...

function Update(_id, data, callback) {    
 elasticClient.get().update({
    index: indexName,
    type: tablename,
    id: _id,
    retry_on_conflict: 5,
    body: {
        doc: data,
        doc_as_upsert: true
    }
 }, (err, results) => {
    if (err) {
        console.log("= = = = [elasticClient Update err]= = = = =", err);
    }
    return callback(err, results)
})

}

Issue: When I call this update function, It's not returning any data... And I got this error...

error :  StatusCodeError: Request Timeout after 30000ms
/node_modules/elasticsearch/src/lib/transport.js:397:9

Note: For Elastic Search connection, I'm using Amazon Elastic Search service and I'm passing its VPC endpoint.

Node version: 12.14.1

Elasticsearch version 6.3

Package.json : "elasticsearch": "16.6.0"

Sachin Shah
  • 4,503
  • 3
  • 23
  • 50
  • Try doing a `curl` to determine if it's an issue with the Node.js library or with the server being unreachable. Something like this: `curl http://myserver:myport/_cat/health` – Daniel Feb 07 '20 at 06:07
  • When I run curl in instance then I got data with status Yellow. But if I fire command from local terminal then I didn't get any res. Actually I'm very new with this so I don't know this is correct or not. – Sachin Shah Feb 07 '20 at 06:15
  • Even though, AWS kibana link is not opening in browser and in AWS console it shows active with green color. – Sachin Shah Feb 07 '20 at 06:16
  • Do you think, Issue should be related to security group? – Sachin Shah Feb 07 '20 at 06:17
  • When you say "local terminal", do you mean a terminal in your laptop? if so, notice ElasticSearch should not be open to the public Internet, as exposing your database like that is prone to data leaks (a single misconfiguration and you're done). Anyways, if from some computer A you expect to be able to connect to ElasticSearch and you are not being able, then the first things I'd check is the security groups and in which VPC/subnet the database is located. If the DB is in a private subnet (as it should be) then it's ok not to be able to access it from outside. – Daniel Feb 07 '20 at 06:21
  • Think of how your security model should work. Usually your ElasticSearch would be in a private subnet, and you would only connect to it from instances that are connected to that subnet and taht are allowed in the rules of your security groups. – Daniel Feb 07 '20 at 06:24
  • @MondKin Yeah, It's issue in AWS security. I tried to create a public domain and it works fine now. Please post your answer so I can accept it. – Sachin Shah Feb 07 '20 at 09:05

1 Answers1

1

When you say "local terminal", do you mean a terminal in your laptop? If so, notice ElasticSearch should not be open to the public Internet, as exposing your database like that is prone to data leaks (a single misconfiguration and you're done).

Anyways, if from some computer A you expect to be able to connect to ElasticSearch and you are not being able, then the first things I'd check are the security groups and in which VPC/subnet the database is located. If the DB is in a private subnet (as it should be) then it's ok not to be able to access it from outside.

Think of how your security model should work. Usually, your ElasticSearch would be in a private subnet, and you would only connect to it from instances that are connected to that subnet and that are allowed in the rules of your security groups.

Daniel
  • 21,933
  • 14
  • 72
  • 101