node.js - 为什么这个 HTTP 请求在 AWS Lambda 上不起作用?

标签 node.js amazon-web-services httprequest aws-lambda

我开始使用 AWS Lambda,并尝试从我的处理程序函数请求外部服务。根据this answer ,HTTP 请求应该可以正常工作,而且我还没有找到任何其他说明的文档。 (其实已经有人发了code that use the Twilio API to send SMS了。)

我的处理程序代码是:

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 行:

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

我希望那里有另一行:

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

但那是缺失的。如果我在本地机器上的 Node 中使用没有处理程序包装器的基本部分,则代码将按预期工作。

我正在使用的 inputfile.txt 用于 invoke-async 调用是这样的:

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

似乎完全跳过了执行请求的处理程序代码部分。我从 request lib 开始并回退到使用普通的 http 创建一个最小的例子。我还尝试请求我控制的服务的 URL 以检查日志并且没有请求进入。

我完全被难住了。 Node 和/或 AWS Lambda 是否有任何原因不执行 HTTP 请求?

最佳答案

当然,我误解了这个问题。 As AWS themselves put it :

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,导致我的函数提前终止。

工作代码是这样的:

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

更新: 从 2017 年开始,AWS 已弃用旧的 Nodejs 0.10,现在只有较新的 4.3 运行时可用(应更新旧功能)。此运行时对处理程序函数进行了一些更改。新的处理程序现在有 3 个参数。

function(event, context, callback)

虽然您仍然会在 context 参数上找到 succeeddonefail,但 AWS 建议使用 callback 函数,或者默认返回 null

callback(new Error('failure')) // to return error
callback(null, 'success msg') // to return ok

完整的文档可以在 http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html 上找到。

关于node.js - 为什么这个 HTTP 请求在 AWS Lambda 上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449363/

相关文章:

node.js - 如何为 Node JS 的 Chrome 专用 DevTools 添加扩展

node.js - npm 5 忽略包锁

amazon-web-services - Jenkins:将应用程序部署到 EC2 实例

java - 如何在 Java 11 中读取 HttpRequest 的正文?

javascript - 如何在 node.js 服务器上接收 "navigator.sendbeacon"发布的数据?

javascript - 使用 Grunt 从单个 Jade 模板创建多个 HTML 文件

amazon-web-services - AWS 自动扩展 : how to disable health checks

php - 使用PHP从S3流音频并支持Range

dart - 实现代理服务器

node.js - NodeJS - 确定目标网站是否使用 HTTP/2