Reputation: 361
I've been using AWS JS SDK
in node
and I wanted to describe my existing ec2
istances in all the regions, but I get an empty reservation[]
. I tried to specify a region using
AWS.config.update{}
and it worked as expected and it returned the instances, but this is what I want. I want to query AWS for all my instances, without specifying a region. Is there a simple way!?
( I'm using my smartphone to ask this question, I can't access my computer right now).
Thank you for your help.
Upvotes: 1
Views: 2737
Reputation: 1392
According to Amazon Web Services docs,
For example, if you need to access Amazon EC2 objects in multiple regions, create an EC2 service object for each region and then set the region configuration of each service object accordingly.
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'});
My implementation,
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":[]}]
I cut off the output for the region I am using because it was really long.
Upvotes: 3
Reputation: 200562
You have to loop through each region, and make the call once for each region. The API is region specific, you can't get the list of all EC2 instances in all regions in a single API call.
Upvotes: 1