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


当前回答

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

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

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

pending
running
stopping
stopped
shutting-down
terminated

其他回答

您可以跨所有区域运行descripbeinstances()。

此外,你可以:

通过Lambda和Cloud watch实现自动化。 使用Lambda和api网关创建api端点,并在代码中使用它

NodeJS中的示例:

创建一个区域(端点)数组。[也可以使用AWS描述区域()]

var regionNames = ['us-west-1', 'us-west-2', 'us-east-1', 'eu-west-1', 'eu-central-1', 'sa-east-1', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'ap-northeast-2'];

    regionNames.forEach(function(region) {
        getInstances(region);
    });

然后,在getInstances函数中,descripbeinstances()可以是 调用。

function getInstances(region) {
            EC2.describeInstances(params, function(err, data) {
                if (err) return console.log("Error connecting to AWS, No Such Instance Found!");
                data.Reservations.forEach(function(reservation) {
                //do any operation intended
      });
    }

当然,你可以随意使用ES6及以上版本。

我写了一个lambda函数来获得任何状态[正在运行,停止]和任何区域的所有实例,还将给出关于实例类型和各种其他参数的详细信息。

脚本在所有AWS区域运行,并调用descripbeinstances()来获取实例。

你只需要用运行时的nodejs创建一个lambda函数。 您甚至可以从中创建API,并在需要时使用它。

此外,您可以查看AWS官方文档用于描述实例,以探索更多选项。

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

一个快速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
...

在控制台

进入VPC面板https://console.aws.amazon.com/vpc/home,单击“运行实例”。->查看所有区域。

在CLI

例如,将此添加到.bashrc。重新加载源代码~/。Bashrc,并运行它

注意:除了aws CLI,您还需要安装jq

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
}

示例输出:

$ 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'..

基于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"
}