2

I am using AWS HttpClient handleRequest inside a lambda function written in Node.Js using aws sdk to Search ElasticSearch url. I am following the AWS Documentation.

https://docs.amazonaws.cn/en_us/elasticsearch-service/latest/developerguide/es-request-signing.html

Please have a look at the code block below. In that one the console log logs only {} and the handle Request method is not being called at all.

var client = new AWS.HttpClient();
//logs only {}
console.log("client", client);

//Not being called and no logs at all
client.handleRequest(request, null, function(response) {
    console.log(response.statusCode + ' ' + response.statusMessage);
    var responseBody = '';
    response.on('data', function (chunk) {
    responseBody += chunk;
    });
    response.on('end', function (chunk) {
    console.log('Response body: ' + responseBody);
    });
    }, function(error) {
     console.log('Error: ' + error);
   });
}

1 Answers1

1

I had the same issue when following a sample code in aws-sample here It seems the repository is in an orphan state now and nobody maintains it.

I am interested in the reason why HttpClient doesn't work as expected. My environment is Lambda nodejs 12.x and aws-sdk version should be 2.x
Solution:
1. Don't use Async handler. Replace it with a Sync function.
2. If you insist using Async, put await before the function where HTTP connection to Elastic search is established.

A good article to explain: https://levelup.gitconnected.com/avoiding-the-pitfalls-of-async-node-js-functions-in-aws-lambda-941220582e7a

You can also use 'elasticsearch' instead. Follow this link for more info.

Jason Kang
  • 21
  • 2
  • Thank you very much Jason. await is what i was missing, your answer really helped at tough time. Thanks Guys. – Mohamed Farish Mar 10 '20 at 23:53
  • Hello! Sorry I don't understand where the`await` should go to. Neither `var client = await new AWS.HttpClient();` nor `await client.handleRequest(...)` works – cedric Sep 04 '20 at 13:31