node.js - Node 能够发出http请求 Node js 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('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回答

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

最佳答案

利用(更新的)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 应该可以正常运行。

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

关于node.js - Node 能够发出http请求 Node js aws lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951099/

相关文章:

node.js - 连续分页

python - Dynamodb - 查询列表是否包含

express - 将图像从 lambda 上传到 s3(损坏的文件)

mysql - 数据库连接卡在 AWS Lambda 上

amazon-web-services - 如何在 EFS 上创建文件夹?

amazon-web-services - 是否可以通过有效负载中的多个事件通过 Event Bridge 触发 AWS Lambda 函数?

node.js - 带 Puppeteer 的 Headless Chromium 不适用于 Amazon Linux AMI

node.js - 有没有办法在 Azure Functions 中做出响应并继续使用 NodeJ?

node.js - 从 sailsjs 模型中的集合中检索特定值

amazon-web-services - 如何使用 !Sub 或 !Join "AWS::CloudFormation::Init",文件 key