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


当前回答

一个快速bash onlineer命令,打印所有区域的所有实例id:

$ aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text |xargs -I {} aws ec2 describe-instances --query Reservations[*].Instances[*].[InstanceId] --output text --region {}

# Example output
i-012344b918d75abcd
i-0156780dad25fefgh
i-0490122cfee84ijkl
...

其他回答

@imTachu解决方案效果很好。要通过AWS控制台做到这一点…

AWS控制台 服务 网络和内容交付 VPC 寻找一个名为“Running Instances”的块,这将显示您当前的区域 点击下方的“查看所有区域”链接

我创建了一个开源脚本,可以帮助您列出所有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

CRUD AWS资源的好工具。找到(EC2 | RDS |我. .]在所有地区。可以对过滤结果执行操作(stop|run|terminate)。

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]

基于@hansaplast代码,我创建了支持多个配置文件作为参数的Windows友好版本。只需将该文件保存为cmd或bat文件。您还需要有jq命令。

@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}"
)

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

详情请参阅公告或文档


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