Elasticsearch npm: Passing AWS Credentials
Figured this out. For future reference, elasticsearch JS does not have a method for signing for AWS. It only supports SSL signed and basic authentication. Therefore you can use this NPM called http-aws-es
. It uses the AWS SDK to sign for Elasticsearch using your IAM access key id and secret access key. Here is what I ended up with for my Elasticsearch client
const elasticsearch = require('elasticsearch');const AWSConnector = require('http-aws-es'),const envs = require('dotenv').config();module.exports = function () { return new elasticsearch.Client({ connectionClass: AWSConnector, apiVersion: '2.3', port: 443, protocol: 'https', host:`${envs.endpoint}`, amazonES: { region: 'us-east-1', accessKey: envs.accessKeyId, secretKey: envs.secretAccessKey } })}
One other thing to note is that I had to set the port to 443
. It would be port 80
if the protocol were http. The Elasticsearch JS library defaults to port 9200
for obvious reasons. If you don't specify the port, it will always default to 9200
regardless of the host you add.
this is the code that worked for me.
const elasticsearch = require('elasticsearch');var AWS = require('aws-sdk');var options = { host: 'host', port:443, protocol:'https', connectionClass: require('http-aws-es'), awsConfig:new AWS.Config({ credentials: new AWS.Credentials('aws_access_key', 'aws_secret_key'), region: 'us-east-1' })};var client = require('elasticsearch').Client(options);client.ping({ // ping usually has a 3000ms timeout requestTimeout: 3000}, function (error) { if (error) { console.trace('elasticsearch cluster is down!'); } else { console.log('All is well'); }});