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

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

    Good tool to CRUD AWS resources. Find [EC2|RDS|IAM..] in all regions. There can do operations (stop|run|terminate) on filters results.

    python3 awsconsole.py ec2 all // return list of all instances
    python3 awsconsole.py ec2 all -r eu-west-1
    python3 awsconsole.py ec2 find -i i-0552e09b7a54fa2cf --[terminate|start|stop]
    
    0 讨论(0)
  • 2021-01-29 23:27

    A non-obvious GUI option is Resource Groups > Tag Editor. Here you can find all instances across all regions, even if the instances were not tagged.


    EDIT: This screen was recently redesigned and now has a new look and an "All regions" option.

    0 讨论(0)
  • 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'..
    
    0 讨论(0)
  • 2021-01-29 23:31

    I created an open-source script that helps you to list all AWS instances. https://github.com/Appnroll/aws-ec2-instances

    That's a part of the script that lists the instances for one profile recording them into an postgreSQL database with using jq for json parsing:

    DATABASE="aws_instances"
    TABLE_NAME="aws_ec2"
    SAVED_FIELDS="state, name, type, instance_id, public_ip, launch_time, region, profile, publicdnsname"
    # collects the regions to display them in the end of script
    REGIONS_WITH_INSTANCES=""
    
    for region in `aws ec2 describe-regions --output text | cut -f3`
    do
       # this mappping depends on describe-instances command output
       INSTANCE_ATTRIBUTES="{
            state: .State.Name,
            name: .KeyName, type: .InstanceType,
            instance_id: .InstanceId,
            public_ip: .NetworkInterfaces[0].Association.PublicIp,
            launch_time: .LaunchTime,
            \"region\": \"$region\",
            \"profile\": \"$AWS_PROFILE\",
            publicdnsname: .PublicDnsName
       }"
    
       echo -e "\nListing AWS EC2 Instances in region:'$region'..."
       JSON=".Reservations[] | ( .Instances[] | $INSTANCE_ATTRIBUTES)"
       INSTANCE_JSON=$(aws ec2 describe-instances --region $region)
    
       if echo $INSTANCE_JSON | jq empty; then
          # "Parsed JSON successfully and got something other than false/null"
          OUT="$(echo $INSTANCE_JSON | jq $JSON)"
    
          # check if empty
          if [[ ! -z "$OUT" ]] then
            for row in $(echo "${OUT}" | jq -c "." ); do
              psql -c "INSERT INTO $TABLE_NAME($SAVED_FIELDS) SELECT $SAVED_FIELDS from json_populate_record(NULL::$TABLE_NAME, '${row}') ON CONFLICT (instance_id)
                DO UPDATE
                SET state = EXCLUDED.state,
                name = EXCLUDED.name,
                type = EXCLUDED.type,
                launch_time = EXCLUDED.launch_time,
                public_ip = EXCLUDED.public_ip,
                profile = EXCLUDED.profile,
                region = EXCLUDED.region,
                publicdnsname = EXCLUDED.publicdnsname
                " -d $DATABASE
            done
    
            REGIONS_WITH_INSTANCES+="\n$region"
          else
            echo "No instances"
          fi
       else
            echo "Failed to parse JSON, or got false/null"
       fi
    done
    
    0 讨论(0)
  • 2021-01-29 23:32

    Based on @hansaplast code I created Windows friendly version that supports multiple profiles as an argument. Just save that file as cmd or bat file. You also need to have jq command.

    @echo off 
    setlocal enableDelayedExpansion
    
    set PROFILE=%1
    IF "%1"=="" (SET PROFILE=default)
    
    echo checkin instances in all regions for %PROFILE% account
    FOR /F "tokens=* USEBACKQ" %%F IN (`aws ec2 describe-regions --query Regions[*].[RegionName] --output text --profile %PROFILE%`) DO (
    echo === region: %%F
    aws ec2 describe-instances --region %%F --profile %PROFILE%| jq ".Reservations[].Instances[] | {type: .InstanceType, state: .State.Name, tags: .Tags, zone: .Placement.AvailabilityZone}"
    )
    
    0 讨论(0)
提交回复
热议问题