简体   繁体   中英

Error connecting AWS ElasticSearch Instance from AWS Lambda function while using Serverless Framework

I am using serverless framework to implement a serverless project. I added some config in my serverless.yml file to create instance for aws elasticsearch service, which is successfully created. Then i created a elasticsearch client in my handler and ping it for testing. So when i ping the elastic search client from serverless offline on my local system, It worked fine and got a 'true' response, but when i deployed the same code on aws lambda than it gets timed out after getting no response till 30 sec.

Have given all policies that can be required but getting no luck.

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.'
    }
 }
}

This way there is no way for aws elasticsearch to know who you are. You need to sign your request before sending it to aws elasticsearch. You can use a package called http-aws-es which basicsally reads the aws credentials from your ec2/lambda and signs the request for you. Your code will look like this

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."
    };
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM