【问题标题】:node able to make http request node js aws lambda节点能够发出http请求节点js aws lambda
【发布时间】:2019-04-30 00:09:12
【问题描述】:

我正在尝试编写一个 lambda 函数,该函数将对在我的 pod 的 ec2 实例中运行的服务的端点进行 3 次 http 调用, aws lambda 将由我配置的 cron 触发, 在配置 aws lambda 时,我还在网络设置中添加了 VPC。

我正在使用 node.js 8.10 来编写我的 lambda 处理函数,这是我的 lambda 处理函数的代码

'use strict';

var http = require('http');

exports.handler = async (event) => {
  http.get('url1', function(res) {
    console.log("Got response: " + res.statusCode);

  }).on('error', function(e) {
    console.log("Got error: " + e.message);

  });
  http.get('url2', function(res) {
    console.log("Got response: " + res.statusCode);

  }).on('error', function(e) {
    console.log("Got error: " + e.message);

  });
  http.get('url3', function(res) {
    console.log("Got response: " + res.statusCode);

  }).on('error', function(e) {
    console.log("Got error: " + e.message);

  });

  console.log('end request to');
}

我也试过了

'use strict';

var http = require('http');

exports.handler = async (event,context) => {
  http.get('url1', function(res) {
    console.log("Got response: " + res.statusCode);
    context.succeed();
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
   context.done(null, 'FAILURE');
  });
  http.get('url2', function(res) {
    console.log("Got response: " + res.statusCode);
    context.succeed();
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
   context.done(null, 'FAILURE');
  });
  http.get('url3', 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');
}

但在这两种情况下我都得到了这个:

START RequestId: 0fa5225f-a54f-11e8-85a9-83174efb4714 Version: $LATEST
2018-08-21T14:32:41.855Z    0fa5225f-a54f-11e8-85a9-83174efb4714    end request to
END RequestId: 0fa5225f-a54f-11e8-85a9-83174efb4714
REPORT RequestId: 0fa5225f-a54f-11e8-85a9-83174efb4714

我参考this回答

有什么原因导致它不工作吗?

【问题讨论】:

  • 您可能正在使用更新版本的 Node.js 运行时(考虑到您对函数声明使用了 async 关键字)。这意味着您真的不想使用 context 来处理 lambda 的执行。相反,我将依赖 async await 功能,并在您的功能完成后简单地使用 return
  • 另外,看看this page,它解释了如何使用更新的运行时。

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


【解决方案1】:

利用(更新的)async/await 功能,并减少样板文件,您可以这样提出您的要求:

const get = async (requestUrl) => {
    return new Promise((resolve, reject) => {
        http.get(requestUrl, function(res) {
            console.log("Got response: " + res.statusCode);
            resolve(res);
        }).on('error', function(e) {
            console.log("Got error: " + e.message);
            reject(e);
        });
    });
}

在您的 lambda 文件中定义该函数,然后您可以在处理函数中调用它,如下所示:

const response1 = await get('url1');

那么你的 lambda 应该可以正常运行了。

有关在 AWS Lambda 中使用 async 函数的更多信息,请参阅 this blog post 从他们将 Node.js 8.10 运行时引入 AWS Lambda(因此允许 async/await 功能)。

【讨论】:

  • 试过这个在本地运行良好,但是当我在 lambda 函数中运行它时超时已经将 lambda 函数超时增加到 50 秒,在本地响应是在 miili 秒内出现但不知道为什么它是计时在 lambda 中输出。
  • 我猜这是权限问题。仔细检查您的 lambda 的 VPC 配置,并确保 EC2 安全组允许访问您为 API 提供服务的端口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 2016-03-14
  • 2017-12-24
  • 2018-06-04
  • 1970-01-01
  • 2017-05-25
相关资源
最近更新 更多