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


当前回答

您可以使用cli的云资源枚举工具(跨地区、跨账号扫描)- https://github.com/scopely-devops/skew

经过简单配置后,您可以使用以下代码列出所有美国AWS区域中的所有实例(假设123456789012是您的AWS帐号)。

from skew import scan

arn = scan('arn:aws:ec2:us-*:123456789012:instance/*')
for resource in arn:
    print(resource.data)

其他回答

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

#!/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最近推出了Amazon EC2全球视图,最初支持实例、vpc、子网、安全组和卷。

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

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

pending
running
stopping
stopped
shutting-down
terminated

每次创建资源时,都用名称标记它,现在您可以使用资源组在所有区域中查找带有名称标记的所有类型的资源。

您可以使用cli的云资源枚举工具(跨地区、跨账号扫描)- https://github.com/scopely-devops/skew

经过简单配置后,您可以使用以下代码列出所有美国AWS区域中的所有实例(假设123456789012是您的AWS帐号)。

from skew import scan

arn = scan('arn:aws:ec2:us-*:123456789012:instance/*')
for resource in arn:
    print(resource.data)

基于imTachus的答案,但更少的啰嗦,加上更快。您需要安装jq和aws-cli。

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

该脚本为每个区域(现在是15个!)并行运行aws ec2 description -instances,并仅从json输出中提取相关的位(状态、标签、可用分区)。需要set +m,以便后台进程在启动/结束时不报告。

示例输出:

{
  "type": "t2.micro",
  "state": "stopped",
  "tags": [
    {
      "Key": "Name",
      "Value": "MyEc2WebServer"
    },
  ],
  "zone": "eu-central-1b"
}