Ankur Lathwal
Ankur Lathwal

Reputation: 725

How to connect to AWS ElasticSearch using npm elasticsearch and http-aws-es?

I am using the npm elasticsearch package to search my AWS ES domain. Everything seems to work fine when I use Postman to make POST requests with my AWS IAM credentials. I wanted to do the same in my code (node.js). I referred to this answer here: How to make calls to elasticsearch apis through NodeJS?

Here is code:

const elasticsearch     = require('elasticsearch');
const awsHttpClient     = require('http-aws-es');
const AWS               = require('aws-sdk');

const client            = new elasticsearch.Client({ 
    host: 'my-aws-es-endpoint', 
    connectionClass: awsHttpClient,
    amazonES: {
        region: 'us-east-1',
        credentials: new AWS.Credentials('my-access-key','my-secret-key')
    }
});

But when I run client.search(), it fails with an error:

Elasticsearch ERROR: 2018-10-31T15:12:22Z
  Error: Request error, retrying
  POST https://my-endpoint.us-east-1.es.amazonaws.com/my-index/student/_search => Data must be a string or a buffer

It also gives me a warning

Elasticsearch WARNING: 2018-10-31T15:12:22Z
  Unable to revive connection: https://my-endpoint.us-east-1.es.amazonaws.com/

When I use just the aws-sdk, it works fine (probably because I sign the request there?)

Can someone suggest what I am I doing wrong here?

Upvotes: 4

Views: 2500

Answers (1)

Ankur Lathwal
Ankur Lathwal

Reputation: 725

I was able to solve this by specifying the region. There is a problem with the elasticsearch client where it's not able to the pick the region which we specify in

amazonES: {
        region: 'us-east-1',
        credentials: new AWS.Credentials('my-access-key','my-secret-key')
    }
}

I solved it by specifying the region using AWS.config.region before the above code

AWS.config.region = 'us-east-1';

Upvotes: 6

Related Questions