简体   繁体   中英

Lambda Function using Node.Js

I am trying to list of all EC2 instances in the particular region using lambda function. 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.

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.

Note, that we pass data variable into resovle instead of just "Data" string, which did not make any sense previously.

Also, keep in mind, that with this code your lambda function will return the raw output from describeInstances response. 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.

TL/DR: try resolving your promise within the callback.

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. So what's probably happening is that you invoke describeInstances (which doesn't block), and then your promise is immediately resolving. After that, your AWS callback is invoked at some point. 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.

Also, maybe have a look here: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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