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

前端 未结 17 1226
囚心锁ツ
囚心锁ツ 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:18

    Based on imTachus answer but less verbose, plus faster. You need to have jq and aws-cli installed.

    set +m
    for region in $(aws ec2 describe-regions --query "Regions[*].[RegionName]" --output text); do 
      aws ec2 describe-instances --region "$region" | jq ".Reservations[].Instances[] | {type: .InstanceType, state: .State.Name, tags: .Tags, zone: .Placement.AvailabilityZone}" &
    done; wait; set -m
    

    The script runs the aws ec2 describe-instances in parallel for each region (now 15!) and extracts only the relevant bits (state, tags, availability zone) from the json output. The set +m is needed so the background processes don't report when starting/ending.

    Example output:

    {
      "type": "t2.micro",
      "state": "stopped",
      "tags": [
        {
          "Key": "Name",
          "Value": "MyEc2WebServer"
        },
      ],
      "zone": "eu-central-1b"
    }
    
    0 讨论(0)
  • 2021-01-29 23:20

    After reading through all the solutions and trying bunch of stuff, the one that worked for me was-

    1. List item
    2. Go to Resource Group
    3. Tag Editor
    4. Select All Regions
    5. Select EC2 Instance in resource type
    6. Click Search Resources

    0 讨论(0)
  • 2021-01-29 23:22

    To run jobs in parallel and use multiple profiles use this script.

    #!/bin/bash
    for i in profile1 profile2
    do
        OWNER_ID=`aws iam get-user --profile $i --output text | awk -F ':' '{print $5}'`
        tput setaf 2;echo "Profile : $i";tput sgr0
        tput setaf 2;echo "OwnerID : $OWNER_ID";tput sgr0
        for region in `aws --profile $i ec2  describe-regions --output text | cut -f4`
        do
            tput setaf 1;echo  "Listing Instances in region $region";tput sgr0
            aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value , InstanceId]' --profile $i --region $region --output text
        done &
    done
    wait
    

    Screenshot:

    0 讨论(0)
  • 2021-01-29 23:22

    I had to terminate the instances from all regions and AWS customer support shared the following links with me

    US East (N. Virginia) ----- https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#Instances

    US East (Ohio) ------------ https://console.aws.amazon.com/ec2/v2/home?region=us-east-2#Instances

    US West (N. California)---- https://console.aws.amazon.com/ec2/v2/home?region=us-west-1#Instances

    US West (Oregon) ---------- https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#Instances

    Canada (Central) ---------- https://console.aws.amazon.com/ec2/v2/home?region=ca-central-1#Instances

    EU (Ireland) -------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-1#Instances

    EU (Frankfurt) ------------ https://console.aws.amazon.com/ec2/v2/home?region=eu-central-1#Instances

    EU (London) --------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-2#Instances

    EU (Paris) --------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-3#Instances

    Asia Pacific (Singapore) -- https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#Instances

    Asia Pacific (Sydney) ----- https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-2#Instances

    Asia Pacific (Seoul) ------ https://console.aws.amazon.com/ec2/v2/home?region=ap-northeast-2#Instances

    Asia Pacific (Tokyo) ------ https://console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#Instances

    Asia Pacific (Mumbai) ----- https://console.aws.amazon.com/ec2/v2/home?region=ap-south-1#Instances

    South America (Sao Paolo) - https://console.aws.amazon.com/ec2/v2/home?region=sa-east-1#Instances

    EU North (Stockholm) - https://eu-north-1.console.aws.amazon.com/ec2/v2/home?region=eu-north-1#Instances:sort=instanceId

    Hope someone might find this helpful

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 23:24

    My script below, based on various tips from this post and elsewhere. The script is easier to follow (for me at least) than the long command lines.

    The script assumes credential profile(s) are stored in file ~/.aws/credentials looking something like:

    [default]
    aws_access_key_id = foobar
    aws_secret_access_key = foobar
    
    [work]
    aws_access_key_id = foobar
    aws_secret_access_key = foobar
    

    Script:

    #!/usr/bin/env bash
    
    #------------------------------------#
    # Script to display AWS EC2 machines #
    #------------------------------------#
    
    # NOTES:
    # o Requires 'awscli' tools (for ex. on MacOS: $ brew install awscli)
    # o AWS output is tabbed - we convert to spaces via 'column' command
    
    
    #~~~~~~~~~~~~~~~~~~~~#
    # Assemble variables #
    #~~~~~~~~~~~~~~~~~~~~#
    
    regions=$(aws ec2 describe-regions --output text | cut -f4 | sort)
    
    query_mach='Reservations[].Instances[]'
    query_flds='PrivateIpAddress,InstanceId,InstanceType'
    query_tags='Tags[?Key==`Name`].Value[]'
    query_full="$query_mach.[$query_flds,$query_tags]"
    
    
    #~~~~~~~~~~~~~~~~~~~~~~~~#
    # Output AWS information #
    #~~~~~~~~~~~~~~~~~~~~~~~~#
    
    # Iterate through credentials profiles
    for profile in 'default' 'work'; do
    
        # Print profile header
        echo -e "\n"
        echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        echo -e "Credentials profile:'$profile'..."
        echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    
        # Iterate through all regions
        for region in $regions; do
    
            # Print region header
            echo -e "\n"
            echo -e "Region: $region..."
            echo -e "--------------------------------------------------------------"
    
            # Output items for the region
            aws ec2 describe-instances    \
              --profile $profile          \
              --region  $region           \
              --query   $query_full       \
              --output  text              \
              | sed     's/None$/None\n/' \
              | sed     '$!N;s/\n/ /'     \
              | column  -t -s $'\t'
    
        done
    done
    
    0 讨论(0)
提交回复
热议问题