简体   繁体   English

Lambda Function 使用 Node.Js

[英]Lambda Function using Node.Js

I am trying to list of all EC2 instances in the particular region using lambda function.我正在尝试使用 lambda function 列出特定区域中的所有 EC2 实例。 Here is my code:这是我的代码:

const AWS = require('aws-sdk');
AWS.config.region = '******';

exports.handler = async function(event) {
  const promise = new Promise(function(resolve, reject) {
        const ec2 = new AWS.EC2();
    
      ec2.describeInstances( function(err, data) {
      console.log("\nIn describe instances:\n");
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log("\n\n" + data + "\n\n"); // successful response
      context.done(null, 'Function Finished!');  
})
      
      resolve("Data");
    })
  return promise;
}

I get Execution result: succeeded but doesn't really return any instances.我得到 Execution result: succeeded 但并没有真正返回任何实例。

const AWS = require('aws-sdk');
AWS.config.region = '******';

exports.handler = async function(event) {
  const promise = new Promise(function(resolve, reject) {
    const ec2 = new AWS.EC2();
    ec2.describeInstances(function(err, data) {
      console.log("\nIn describe instances:\n");
      if (err) {
        console.log(err, err.stack); // an error occurred
        reject(err);
      } else {
        console.log("\n\n" + data + "\n\n"); // successful response
        resolve(data);
      }    
   }) 
 });

 return promise;
}

So here we have a promise, which is going to be resolved with data in case of success and rejected with an error in case of fail of describing instances.所以这里我们有一个 promise,如果描述实例成功,它将用数据解决,如果描述实例失败,则会用错误拒绝。

Note, that we pass data variable into resovle instead of just "Data" string, which did not make any sense previously.请注意,我们将data变量传递给resovle而不仅仅是“Data”字符串,这在以前没有任何意义。

Also, keep in mind, that with this code your lambda function will return the raw output from describeInstances response.另外,请记住,使用此代码,您的 lambda function 将从describeInstances响应返回原始 output。 If you need to filter some data or add other logic, you need to do that before resolve and pass result into resolve , or await for result of promise, do whatever you need with the data and return the result.如果您需要过滤某些数据或添加其他逻辑,您需要在resolve之前执行此操作并将结果传递给resolve ,或者await promise 的结果,对数据执行任何您需要的操作并返回结果。

TL/DR: try resolving your promise within the callback. TL/DR:尝试回调中解决您的 promise。

Maybe have a bit of a mixup there with callbacks an promises.也许与回调和承诺有一些混淆。

  • Your describeInstances method gets a callback that is being invoked, but you don't know when that will be.您的describeInstances方法获取一个正在调用的回调,但您不知道何时会调用。 So what's probably happening is that you invoke describeInstances (which doesn't block), and then your promise is immediately resolving.所以可能发生的是你调用 describeInstances (它不会阻塞),然后你的 promise 立即解决。 After that, your AWS callback is invoked at some point.之后,您的 AWS 回调会在某个时间点被调用。 I guess that's not what you want?我想这不是你想要的?
  • your function doesn't need the async keyword I guess as you return a promise (you're not awaiting anything.我猜你的 function 不需要async关键字,因为你返回 promise (你没有在等待任何东西。

Also, maybe have a look here: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original另外,也许看看这里: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original

const AWS = require("aws-sdk");
const ec2 = new AWS.EC2({ region: "your_region" });

exports.handler = async function (event) {
  try {
    let response = await ec2.describeInstances().promise();
    return response;
  } catch (error) {
    return error;
  }
};

The above code works neatly for the use-case and doesn't need to deal with promise and callbacks good sir.上面的代码很好地适用于用例,不需要处理 promise 和回调好先生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM