How to see all running Amazon EC2 instances across all regions?

前端 未结 17 1567
囚心锁ツ
囚心锁ツ 2021-01-29 23:03

I switch instances between different regions frequently and sometimes I forget to turn off my running instance from a different region. I couldn\'t find any way to see all the r

17条回答
  •  轮回少年
    2021-01-29 23:24

    You can run DescribeInstances() across all regions.

    Additionally, you can:

    • Automate it through Lambda and Cloud watch.
    • Create api endpoint using Lambda and api gateway and use it in your code

    A sample in NodeJS:

    • Create and array of regions (endpoints). [can also use AWS describeRegions() ]
    var regionNames = ['us-west-1', 'us-west-2', 'us-east-1', 'eu-west-1', 'eu-central-1', 'sa-east-1', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'ap-northeast-2'];
    
        regionNames.forEach(function(region) {
            getInstances(region);
        });
    
    
    • Then, in getInstances function, DescribeInstances() can be called.
    function getInstances(region) {
                EC2.describeInstances(params, function(err, data) {
                    if (err) return console.log("Error connecting to AWS, No Such Instance Found!");
                    data.Reservations.forEach(function(reservation) {
                    //do any operation intended
          });
        }
    

    And Off Course, feel free to use ES6 and above.

    I wrote a lambda function to get you all the instances in any state [running, stopped] and from any regions, will also give details about instance type and various other parameters.

    The Script runs across all AWS regions and calls DescribeInstances(), to get the instances.

    You just need to create a lambda function with run-time nodejs. You can even create API out of it and use it as and when required.

    Additionally, You can see AWS official Docs For DescribeInstances to explore many more options.

提交回复
热议问题