我经常在不同的区域之间切换实例,有时我忘记从不同的区域关闭正在运行的实例。我找不到任何方法来查看Amazon主机上所有正在运行的实例。 是否有任何方法可以显示所有正在运行的实例而不考虑区域?


当前回答

在阅读了所有的解决方案并尝试了一堆东西之后,对我有效的一个是-

列表项 进入资源组 标签编辑器 选择所有区域 在资源类型中选择“EC2实例” 单击“搜索资源”

其他回答

编辑:AWS最近推出了亚马逊EC2全球视图,初步支持实例、vpc、子网、安全组和卷。

详情请参阅公告或文档


一个不明显的GUI选项是Resource Groups控制台中的Tag Editor。在这里,您可以找到所有区域的所有实例,即使这些实例没有被标记。

要并行运行作业并使用多个概要文件,请使用此脚本。

#!/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

截图:

下面是我的脚本,基于这篇文章和其他地方的各种技巧。脚本比长命令行更容易理解(至少对我来说)。

脚本假设凭据概要文件存储在文件~/中。Aws /凭证看起来像这样:

[default]
aws_access_key_id = foobar
aws_secret_access_key = foobar

[work]
aws_access_key_id = foobar
aws_secret_access_key = foobar

脚本:

#!/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

我创建了一个开源脚本,可以帮助您列出所有AWS实例。https://github.com/Appnroll/aws-ec2-instances

这是脚本的一部分,列出了一个配置文件的实例,并使用jq进行json解析,将它们记录到postgreSQL数据库中:

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

AWS最近推出了Amazon EC2全球视图,最初支持实例、vpc、子网、安全组和卷。

进入EC2或VPC控制台,单击左上角的“EC2全局视图”,查看所有正在运行的实例。

然后单击“全局搜索”选项卡,按资源类型筛选并选择实例。不幸的是,这将显示所有状态的实例:

pending
running
stopping
stopped
shutting-down
terminated