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

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

    In Console

    Go to VPC dashboard https://console.aws.amazon.com/vpc/home and click on Running instances -> See all regions.

    In CLI

    Add this for example to .bashrc. Reload it source ~/.bashrc, and run it

    Note: Except for aws CLI you need to have jq installed

    function aws.print-all-instances() {
      REGIONS=`aws ec2 describe-regions --region us-east-1 --output text --query Regions[*].[RegionName]`
      for REGION in $REGIONS
      do
        echo -e "\nInstances in '$REGION'..";
        aws ec2 describe-instances --region $REGION | \
          jq '.Reservations[].Instances[] | "EC2: \(.InstanceId): \(.State.Name)"'
      done
    }
    

    Example output:

    $ aws.print-all-instances 
    
    Listing Instances in region: 'eu-north-1'..
    "EC2: i-0548d1de00c39f923: terminated"
    "EC2: i-0fadd093234a1c21d: running"
    
    Listing Instances in region: 'ap-south-1'..
    
    Listing Instances in region: 'eu-west-3'..
    
    Listing Instances in region: 'eu-west-2'..
    
    Listing Instances in region: 'eu-west-1'..
    
    Listing Instances in region: 'ap-northeast-2'..
    
    Listing Instances in region: 'ap-northeast-1'..
    
    Listing Instances in region: 'sa-east-1'..
    
    Listing Instances in region: 'ca-central-1'..
    
    Listing Instances in region: 'ap-southeast-1'..
    
    Listing Instances in region: 'ap-southeast-2'..
    
    Listing Instances in region: 'eu-central-1'..
    
    Listing Instances in region: 'us-east-1'..
    
    Listing Instances in region: 'us-east-2'..
    
    Listing Instances in region: 'us-west-1'..
    
    Listing Instances in region: 'us-west-2'..
    

提交回复
热议问题