在节点中使用AWS JS SDK来描述所有ec2实例

我已经在node使用了AWS JS SDK ,并且想要描述所有区域中现有的ec2 istances,但是我得到一个空的reservation[] 。 我尝试使用AWS.config.update{}指定一个区域,它按预期工作,并返回实例,但这是我想要的。 我想查询AWS的所有实例,而不指定区域。 有一个简单的方法! (我用我的智能手机问这个问题,我现在无法访问我的电脑)。

感谢您的帮助。

您必须遍历每个地区,并为每个地区拨打一次电话。 API是区域特定的,您无法在单个API调用中获取所有区域中的所有EC2实例的列表。

根据Amazon Web Services文档 ,

例如,如果您需要访问多个区域中的Amazon EC2对象,请为每个区域创build一个EC2服务对象,然后相应地设置每个服务对象的区域configuration。

 var ec2_regionA = new AWS.EC2({region: 'ap-southeast-2', maxRetries: 15, apiVersion: '2014-10-01'}); var ec2_regionB = new AWS.EC2({region: 'us-east-1', maxRetries: 15, apiVersion: '2014-10-01'}); 

我的实现,

 var AWS = require('aws-sdk'); var EC2Objects = [ new AWS.EC2({apiVersion: '2016-11-15',region: 'us-east-1'}), //N. Virginia new AWS.EC2({apiVersion: '2016-11-15',region: 'us-east-2'}), //Ohio new AWS.EC2({apiVersion: '2016-11-15',region: 'us-west-1'}), //N. California new AWS.EC2({apiVersion: '2016-11-15',region: 'us-west-2'}), //Oregon new AWS.EC2({apiVersion: '2016-11-15',region: 'ca-central-1'}), //Canada (Central) new AWS.EC2({apiVersion: '2016-11-15',region: 'eu-west-1'}), //Ireland new AWS.EC2({apiVersion: '2016-11-15',region: 'eu-central-1'}), //Frankfurt new AWS.EC2({apiVersion: '2016-11-15',region: 'eu-west-2'}), //London new AWS.EC2({apiVersion: '2016-11-15',region: 'ap-northeast-1'}), //Tokyo new AWS.EC2({apiVersion: '2016-11-15',region: 'ap-northeast-2'}), //Seoul new AWS.EC2({apiVersion: '2016-11-15',region: 'ap-southeast-1'}), //Singapore new AWS.EC2({apiVersion: '2016-11-15',region: 'ap-southeast-2'}), //Syndney new AWS.EC2({apiVersion: '2016-11-15',region: 'ap-south-1'}), //Mumbai new AWS.EC2({apiVersion: '2016-11-15',region: 'sa-east-1'}) //Sao Paulo ]; var instances = []; listEc2(); function listEc2(){ var params = {}; for(var i=0; i<EC2Objects.length; i++){ EC2Objects[i].describeInstances(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else ec2ListBuilderCallback(data); // successful response }); } } function ec2ListBuilderCallback(data){ instances.push(data); if(instances.length == EC2Objects.length){ console.log(JSON.stringify(instances)); } }