Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点能够发出http请求节点js aws lambda_Node.js_Amazon Web Services_Aws Lambda - Fatal编程技术网

Node.js 节点能够发出http请求节点js aws lambda

Node.js 节点能够发出http请求节点js aws lambda,node.js,amazon-web-services,aws-lambda,Node.js,Amazon Web Services,Aws Lambda,我正在尝试编写一个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(

我正在尝试编写一个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
我提到了他的答案

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

利用(最近的)功能,并减少样板文件,您可以这样提出请求:

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
函数的更多信息,请参阅他们在AWS Lambda中引入Node.js 8.10运行时(从而允许
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
函数的更多信息,请参见他们在AWS Lambda中引入Node.js 8.10运行时(从而允许
async/await
功能)时的内容。

您可能正在使用Node.js运行时的最新版本(考虑到您在函数声明中使用了
async
关键字)。这意味着您实际上不想使用
上下文
来处理lambda的执行。相反,我会依赖于功能,在函数完成后简单地
返回
。另外,请看一看如何使用较新的运行时。您可能正在使用Node.js运行的最新版本时间(考虑到您在函数声明中使用了
async
关键字)。这意味着您真的不想使用
上下文
来处理lambda的执行。相反,我会依赖功能,在您的函数完成后简单地
返回
。另外,请看一看其中解释了如何使用最近的运行时。在本地运行时尝试了此操作,但运行时超时lambda函数内部已将lambda函数超时时间增加到50秒,in local response将在miili秒内发出,但不确定lambda中超时的原因。我猜这是权限问题。请仔细检查lambda的VPC配置,并确保EC2安全组允许访问您所服务的端口g您的API。在本地运行时,尝试了此操作,但在lambda函数内运行时超时。已将lambda函数超时时间增加到50秒,在本地响应时间为mili秒,但不确定它在lambda中超时的原因。我猜这是权限问题。请仔细检查lambda的VPC配置,并确保EC2安全组允许访问为API提供服务的端口。