2016-08-11 4 views
1

AWS JS SDKnodeに追加しました。既存のec2のすべての領域について説明したいと思いますが、空のreservation[]があります。私は AWS.config.update{}を使用して領域を指定しようとしましたが、期待どおりに動作し、インスタンスを返しましたが、これが欲しいものです。リージョンを指定せずに、すべてのインスタンスについてAWSに照会したいと思います。簡単な方法がありますか? (私はスマートフォンを使用してこの質問をしていますが、今は自分のコンピュータにアクセスできません)。ノード内のAWS JS SDKを使用してすべてのec2インスタンスを記述する

ありがとうございました。

答えて

1

各地域をループし、地域ごとに1回電話をかけなければなりません。 APIは地域固有のため、単一のAPI呼び出しですべての地域のすべてのEC2インスタンスのリストを取得することはできません。 Amazon Web Services docsによると

1

、例えば

、あなたは、複数で 領域のAmazon EC2オブジェクトにアクセスする各地域のEC2サービスオブジェクトを作成し、それに応じて、各サービスオブジェクトの 領域設定を設定する必要がある場合。

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)); 
 
    } 
 
}

OUTPUT:

[{"Reservations":[]},{"Reservations":[{"ReservationId":"r-0e7c0a2e3cf30944c","Groups":[],"Instances":[{"InstanceId":"i-0391f0e44b04675ad","ImageId":"ami-0b33d91d","State":{"Code":16,"Name":"running"}],{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]},{"Reservations":[]}] 

それは本当に長かったので、私は私が使用しています地域のために出力を遮断。

関連する問題