Shank
Shank

Reputation: 414

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.

Upvotes: 2

Views: 863

Answers (3)

ydvsailendar
ydvsailendar

Reputation: 40

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.

Upvotes: 0

Philipp Sumi
Philipp Sumi

Reputation: 987

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 asynckeyword 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

Upvotes: 1

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

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.

Upvotes: 1

Related Questions