节点能够使http请求节点js aws lambda [英] node able to make http request node js aws lambda

查看:77
本文介绍了节点能够使http请求节点js aws lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a lambda function which will make 3 http calls to the endpoints of my service running in the ec2 instance of my pod , the aws lambda will be triggered by the cron which I have configured, I have also added the VPC in the network setting while configuring the aws lambda.

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

I am using node.js 8.10 to code my lambda handler function , here is my code for the lambda handler function

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

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

but in both the cases I get this:

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

我提到了答案

有什么原因不起作用吗?

Is there any reason why it is not working ?

推荐答案

利用(最新的)

Taking advantage of the (more recent) async/await functionality, and also cutting down on boilerplate, you could make your requests like so:

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文件中定义该函数,然后可以在处理程序函数中调用它,如下所示:

Define that function in your lambda file and then you can call it within the handler function like so:

const response1 = await get('url1');

然后,您的lambda应该可以正常运行.

Then your lambda should run properly.

有关在AWS Lambda上使用async函数的更多信息,请参见

For more info on using async functions with AWS Lambda see this blog post from when they introduced the Node.js 8.10 runtime to AWS Lambda (thus allowing async/await functionality).

这篇关于节点能够使http请求节点js aws lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆