【问题标题】:Error connecting AWS ElasticSearch Instance from AWS Lambda function while using Serverless Framework使用无服务器框架时从 AWS Lambda 函数连接 AWS ElasticSearch 实例时出错
【发布时间】:2020-05-28 02:24:18
【问题描述】:

我正在使用无服务器框架来实现无服务器项目。我在 serverless.yml 文件中添加了一些配置来为 aws elasticsearch 服务创建实例,该服务已成功创建。然后我在我的处理程序中创建了一个弹性搜索客户端并对其进行 ping 测试。 因此,当我在本地系统上从无服务器离线 ping 弹性搜索客户端时,它工作正常并得到“真实”响应,但是当我在 aws lambda 上部署相同的代码时,它在 30 秒前没有响应后超时。

已经给出了所有可能需要的政策,但没有成功。

Serverless.yml:-

ElasticSearchInstance:
  Type: AWS::Elasticsearch::Domain
  Properties:
    EBSOptions:
      EBSEnabled: true
      VolumeType: gp2
      VolumeSize: 10
    ElasticsearchClusterConfig:
      InstanceType: t2.small.elasticsearch
      InstanceCount: 1
      DedicatedMasterEnabled: false
      ZoneAwarenessEnabled: false
    ElasticsearchVersion: 7.1

Handler.js:-

var {Client} = require('elasticsearch');
var client = new Client({
 host: 'Aws elasticsearch endpoint',
 log: 'trace'
});

module.exports.elasticSearchPing = async () => {
  try {
  console.log('Inside elasticSearchPing function!!!!');
  const res = await client.ping({requestTimeout: 900000});
  console.log('Res: ', res);
  return {
  statusCode: 200,
  body: JSON.stringify({ message: 'Connection successful with elasticSearch.' })
 }
} catch (err) {
  console.log('err: ', err);
  return {
    statusCode: err.statusCode || 500,
    headers: { 'Content-Type': 'text/plain' },
    body: 'Error connecting elasticsearch.'
    }
 }
}

【问题讨论】:

    标签: node.js aws-lambda amazon-cognito serverless-framework aws-elasticsearch


    【解决方案1】:

    这样,aws elasticsearch 就无法知道您是谁。在将请求发送到 aws elasticsearch 之前,您需要对其进行签名。您可以使用一个名为 http-aws-es 的包,它基本上从您的 ec2/lambda 读取 aws 凭证并为您签署请求。您的代码将如下所示

    const {Client} = require("elasticsearch");
    const esConnectionClass = require("http-aws-es");
    
    const client = new Client({
      "host": "Aws elasticsearch endpoint",
      "log": "trace",
      "connectionClass": esConnectionClass
    });
    
    module.exports.elasticSearchPing = async () => {
      try {
        console.log("Inside elasticSearchPing function!!!!");
        const res = await client.ping({"requestTimeout": 900000});
        console.log("Res: ", res);
        return {
          "statusCode": 200,
          "body": JSON.stringify({"message": "Connection successful with elasticSearch."})
        };
      } catch (err) {
        console.log("err: ", err);
        return {
          "statusCode": err.statusCode || 500,
          "headers": {"Content-Type": "text/plain"},
          "body": "Error connecting elasticsearch."
        };
      }
    };
    
    

    【讨论】:

    • 我也尝试过使用 http-aws-es 模块,但它并没有解决问题。实际上问题出在 VPC 子网标识符上,我提供了安全组标识符和子网标识符,但是当我注释掉子网标识符时,它开始正常工作,尽管我所有的子网标识符都是公共的。仍然想知道为什么会这样。以下是 vpc 详细信息:- vpc: securityGroupIds: - ${self:custom.secrets.SECURITY_GROUP_ID} #subnetIds: # - ${self:custom.secrets.SUBNET1_ID} # - ${self:custom.secrets.SUBNET2_ID} # - ${self:custom.secrets.SUBNET3_ID}
    • 你在 vpc 中还是在 vpc 之外也可用?
    • 为 ES 使用 VPC
    猜你喜欢
    • 2019-07-23
    • 2021-01-07
    • 2018-06-28
    • 2023-03-16
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    相关资源
    最近更新 更多