使用AWS的elasticsearch.js

时间:2017-08-30 11:13:36

标签: javascript node.js amazon-web-services elasticsearch

我正在使用 AWS Elasticsearch 服务,并希望通过elasticsearch.js进行连接,但需要一个端口。

看起来AWS只提供 REST API (例如通过卷曲),在端口80上运行。我的群集已启动,我可以通过浏览器访问,但不能通过elasticsearch.js访问。< / p>

此示例对我不起作用:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200', // not working: '', 80, default: 443
  log: 'trace'
});

client.ping({
  requestTimeout: 1000
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});

我找到了http-aws-es,但它也无效。

有什么想法吗?提前谢谢!

5 个答案:

答案 0 :(得分:6)

对于elasticsearch.client,您可以将http-aws-es用于connectionClass,将amazonES用于键。

var client = new elasticsearch.Client({
    hosts: config.elasticsearch.host,
    connectionClass: require('http-aws-es'),
    amazonES: {
        region: config.aws.region,
        accessKey: config.aws.key,
        secretKey: config.aws.secret
    }
});

答案 1 :(得分:3)

NPM软件包elasticsearch已被deprecated替换为@elastic/elasticsearch

因此,您可以考虑使用软件包@acuris/aws-es-connection,它是新的Elasticsearch客户端{{的AWS ES连接),而不是使用应该与已弃用的http-aws-es软件包一起使用的elasticsearch。 1}}。在我的项目中,它对我来说效果很好。您可以在its readme file中找到其用法,但这是一段示例代码:

@elastic/elasticsearch

答案 2 :(得分:2)

这是我一直在使用TypeScript的Connection类的实现:

import { Connection as UnsignedConnection } from '@elastic/elasticsearch';
import * as AWS from 'aws-sdk';
import RequestSigner from 'aws-sdk/lib/signers/v4';
import { ClientRequest, IncomingMessage } from 'http';

class AwsElasticsearchError extends Error {}
type RequestOptions = Parameters<UnsignedConnection['request']>[0];


class AwsSignedConnection extends UnsignedConnection {
  public request(
    params: RequestOptions,
    callback: (err: Error | null, response: IncomingMessage | null) => void,
  ): ClientRequest {
    const signedParams = this.signParams(params);
    return super.request(signedParams, callback);
  }

  private signParams(params: RequestOptions): RequestOptions {
    const region = AWS.config.region || process.env.AWS_DEFAULT_REGION;
    if (!region) throw new AwsElasticsearchError('missing region configuration');
    if (!params.method) throw new AwsElasticsearchError('missing request method');
    if (!params.path) throw new AwsElasticsearchError('missing request path');
    if (!params.headers) throw new AwsElasticsearchError('missing request headers');

    const endpoint = new AWS.Endpoint(this.url.href);
    const request = new AWS.HttpRequest(endpoint, region);

    request.method = params.method;
    request.path = params.querystring
      ? `${params.path}/?${params.querystring}`
      : params.path;
    request.body = params.body;

    Object.entries(params.headers).forEach(([header, value]) => {
      if (value === undefined) return;
      if (typeof value === 'string') request.headers[header] = value;
      else if (typeof value === 'number') request.headers[header] = `${value}`;
      else request.headers[header] = value.join('; ');
    });
    request.headers.Host = endpoint.host;

    const signer = new RequestSigner(request, 'es');
    signer.addAuthorization(AWS.config.credentials, new Date());
    return request;
  }
}

export { AwsSignedConnection, UnsignedConnection, AwsElasticsearchError };

然后,仅在凭据可用时才可以提供它,因此您可以使用它指向没有凭据的本地(例如Docker)Elasticsearch:

import awsSdk from 'aws-sdk';
import elasticsearch from '@elastic/elasticsearch';
import { AwsSignedConnection, UnsignedConnection } from '../aws-es-connector';

client = new elasticsearch.Client({
  Connection: awsSdk.config.credentials ? AwsSignedConnection : UnsignedConnection,
  node: elasticsearchEndpoint,
});

答案 3 :(得分:1)

我花了一个小时来找到对我有用的方法。唯一的一项工作是:

var elasticsearch = require('elasticsearch');
var AWS = require('aws-sdk');
var connectionClass = require('http-aws-es');
AWS.config.update({
    credentials: new AWS.Credentials('accessKey','secret'),
    region: 'us-east-1'
  });

var client = new elasticsearch.Client({
    host: 'https://yourdomainurl.us-east-1.es.amazonaws.com',
    log: 'debug',
    connectionClass: connectionClass,
    amazonES: {
      credentials: new AWS.EnvironmentCredentials('AWS')
    }
  });

确保已为IAM用户添加访问策略。然后一切都应该正常工作。 希望这会对别人有所帮助。

答案 4 :(得分:0)

Elasticsearch Service端点在HTTPS(端口443)或HTTP(端口80)上运行。

您可以将localhost:9200交换到此,以进行简单的替换替换