AWS.HttpClient handleRequest在AWS Lambda中不起作用 [英] AWS.HttpClient handleRequest is not working in AWS lambda

查看:91
本文介绍了AWS.HttpClient handleRequest在AWS Lambda中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Node.Js编写的lambda函数中使用AWS HttpClient handleRequest ,使用 aws sdk 来搜索ElasticSearch url.我正在关注AWS文档.

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

请查看下面的代码块.在那个控制台日志中仅记录{},并且根本不调用handle Request方法.

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);
   });
}

推荐答案

在aws-sample

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.

我对HttpClient无法按预期工作的原因感兴趣.我的环境是Lambda nodejs 12.x,aws-sdk版本应为2.x
解决方案:
1.不要使用异步处理程序.将其替换为同步功能.
2.如果您坚持使用Async,请在建立与Elastic search的HTTP连接的函数之前放置 await .

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.

一篇很好的文章来解释: https://levelup.gitconnected.com/avoiding-the-pitfalls-of-async-node-js-functions-in-aws-lambda-941220582e7a

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

您也可以改用"elasticsearch".点击链接以获取更多信息.

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

这篇关于AWS.HttpClient handleRequest在AWS Lambda中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆