这是为什么HTTP请求不工作的AWS LAMBDA?这是、工作、LAMBDA、HTTP

2023-09-11 10:24:13 作者:灵魂焚毁一座荒芜

我开始使用AWS的Lambda和我想从我的处理函数请求外部服务。根据这个答案,HTTP请求应该工作得很好,而且我还没有发现任何文件,指出并非如此。 (事实上​​,人们已经发布了使用Twilio API发送短信 code)

I'm getting started with AWS Lambda and I'm trying to request an external service from my handler function. According to this answer, HTTP requests should work just fine, and I haven't found any documentation that says otherwise. (In fact, people have posted code that use the Twilio API to send SMS.)

我的处理程序code是:

My handler code is:

var http = require('http');

exports.handler = function(event, context) {
  console.log('start request to ' + event.url)
  http.get(event.url, function(res) {
    console.log("Got response: " + res.statusCode);
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });

  console.log('end request to ' + event.url)
  context.done(null);
}

和我看到我的CloudWatch记录以下4行:

and I see the following 4 lines in my CloudWatch logs:

2015-02-11 07:38:06 UTC START RequestId: eb19c89d-b1c0-11e4-bceb-d310b88d37e2
2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 start request to http://www.google.com
2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 end request to http://www.google.com
2015-02-11 07:38:06 UTC END RequestId: eb19c89d-b1c0-11e4-bceb-d310b88d37e2

我期望另一行中有:

I'd expect another line in there:

2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 Got response: 302

但唯一缺少的。如果我使用的重要组成部分,而不处理器封装在节点上我的本地机器中,code正常工作。

but that's missing. If I'm using the essential part without the handler wrapper in node on my local machine, the code works as expected.

inputfile.txt 我使用的是为引用-异步调用是这样的:

The inputfile.txt I'm using is for the invoke-async call is this:

{
   "url":"http://www.google.com"
}

这似乎是的处理程序code,做请求被完全忽略的部分。我开始了与要求LIB ,然后回落到使用普通的 HTTP 来创建一个小例子。我也试着要求我控制检查日志服务的URL并没有请求进入。

It seems like the part of the handler code that does the request is skipped entirely. I started out with the request lib and fell back to using plain http to create a minimal example. I've also tried to request a URL of a service I control to check the logs and there's no requests coming in.

我完全难住了。 是否有任何理由节点和/或AWS LAMBDA不会执行HTTP请求?

推荐答案

当然,我被误解的问题。 作为AWS自己把它:

Of course, I was misunderstanding the problem. As AWS themselves put it:

有关首次在lambda那些遇到nodejs,一个共同的   错误是忘记了回调异步执行,并呼吁    context.done()的时候,你真的打算等原始处理程序   对于另一个回调(诸如S3.PUT操作)来完成,从而迫使   功能终止其工作不完整的。

For those encountering nodejs for the first time in Lambda, a common error is forgetting that callbacks execute asynchronously and calling context.done() in the original handler when you really meant to wait for another callback (such as an S3.PUT operation) to complete, forcing the function to terminate with its work incomplete.

我打电话 context.done 的任何回调请求之前的方式解雇,造成时间我的函数的提前终止

I was calling context.done way before any callbacks for the request fired, causing the termination of my function ahead of time.

工作code是这样的:

The working code is this:

var http = require('http');

exports.handler = function(event, context) {
  console.log('start request to ' + event.url)
  http.get(event.url, function(res) {
    console.log("Got response: " + res.statusCode);
    context.succeed();
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
    context.done(null, 'FAILURE');
  });

  console.log('end request to ' + event.url);
}