简体   繁体   English

如何检查Node.JS AWS Lambda数据?

[英]How to inspect Node.JS AWS Lambda data?

I am new to both Lambda and Node.JS. 我对Lambda和Node.JS都是陌生的。 I originally wanted to write the function in Python, but boss says he'd like it in Node. 我最初想用Python编写函数,但老板说他希望在Node中使用它。 I am writing an AWS Lambda function to turn off specified EC2 instances at the end of the day. 我正在编写一个AWS Lambda函数,以在一天结束时关闭指定的EC2实例。 I am having trouble inspecting if describeInstances is grabbing the correct data. 我在检查describeInstances是否获取正确的数据时遇到麻烦。

Right now the code shows return String(instances); 现在,代码显示return String(instances); but I've tried numerous different things such as return instances.response.data; 但我尝试了许多不同的操作,例如return instances.response.data; which gives an error about trying to stringify the data or something. 这会给出有关尝试对数据或某些内容进行字符串化处理的错误。

var AWS = require('aws-sdk');
var ec2 = new AWS.EC2();
AWS.config.update({region: 'us-west-2'});

exports.handler = async (event) => {

 var params = {
  Filters: [
     {
    Name: "tag:Parking", 
    Values: [
       "true"
    ]
   }
  ]
 };

var instances = ec2.describeInstances(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else return data; // successful response
 });

 //return Object.getOwnPropertyNames(instances);
 //return instances.response.httpResponse;
 return String(instances);
};

I just want to be able to view a list of the returned EC2 instances to see if I have the correct instances before turning them off. 我只希望能够查看返回的EC2实例的列表,以在关闭它们之前查看我是否具有正确的实例。

describeInstances returns an AWS.Request object not the actual results of the operation, which are returned in the callback handler you passed to the describeInstances function. describeInstances返回一个AWS.Request对象,而不是操作的实际结果,该结果将在您传递给describeInstances函数的回调处理程序中返回。

You can do it like this using async/await syntax 您可以使用async/await语法来做到这一点

const data = await ec2.describeInstances(params).promise();
return data

You should be able to view the logs in the AWS Console. 您应该能够在AWS控制台中查看日志。 The easiest way is to go to the Lambda console, select the function in question, click "Monitoring" near the top left, and then "View logs in CloudWatch" just below that to the right. 最简单的方法是转到Lambda控制台,选择有问题的功能,单击左上方附近的“监视”,然后单击右下方的“查看CloudWatch中的日志”。

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

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