我正在使用下面的node.js代码列出我的AWS实例。但是,它返回了很多我不感兴趣的数据。我只想检索dns名称和ip地址。我当然可以过滤它们,但是是否可以将检索范围限制为仅这两个字段?
var AWS = require('aws-sdk');
exports.handler = function (event, context) {
console.log("\n\nLoading handler\n\n");
var 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" + JSON.stringify(data) + "\n\n"); // successful response
context.done(null, 'Function Finished!');
});
};
答案 0 :(得分:1)
否,您无法向EC2发送自定义请求,这将导致EC2仅返回您感兴趣的属性(IP地址和公共DNS)。
您可能必须使用对象解构或map()或lodash从结果中过滤掉不需要的属性。也许是这样的:
const _ = require('lodash');
const rc = _.flatten(data.Reservations.map((r) => {
return r.Instances.map((i) => {
return _.pick(i, ['PublicDnsName', 'PublicIpAddress']);
});
}));
这将产生类似的内容:
[
{"PublicDnsName":"ec2-52-1-1-1.compute-1.amazonaws.com","PublicIpAddress":"52.1.1.1"},
{"PublicDnsName":"ec2-52-2-2-2.compute-1.amazonaws.com","PublicIpAddress":"52.2.2.2"},
{"PublicDnsName":"ec2-52-3-3-3.compute-1.amazonaws.com","PublicIpAddress":"52.3.3.3"},
]