簡體   English   中英

在節點中使用AWS JS SDK來描述所有ec2實例

[英]Use AWS JS SDK in node to describe all ec2 instances

我一直在node使用AWS JS SDK ,我想在所有地區描述我現有的ec2 istances,但我得到一個空的reservation[] 我嘗試使用AWS.config.update{}指定一個區域,它按預期工作,並返回實例,但這就是我想要的。 我想查詢AWS我的所有實例,而不指定區域。 有一個簡單的方法!? (我正在使用我的智能手機提出這個問題,我現在無法訪問我的電腦)。

謝謝您的幫助。

根據亞馬遜網絡服務文檔

例如,如果需要訪問多個區域中的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":[]}]

我切斷了我正在使用的區域的輸出,因為它真的很長。

您必須遍歷每個區域,並為每個區域撥打一次電話。 API是特定於區域的,您無法在單個API調用中獲取所有區域中的所有EC2實例的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM