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


当前回答

您可以跨所有区域运行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官方文档用于描述实例,以探索更多选项。

其他回答

基于@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 GUI中无法做到这一点。但是这里有一种方法可以用AWS CLI列出所有地区的所有实例:

for region in `aws ec2 describe-regions --region us-east-1 --output text | cut -f4`
do
     echo -e "\nListing Instances in region:'$region'..."
     aws ec2 describe-instances --region $region
done

从这里开始(如果你想看完整的讨论)

另外,如果你得到一个

必须指定一个区域。您也可以通过运行“aws configure”来配置您的区域。

你可以通过aws配置set region us-east-1来实现,谢谢@Sabuncu的评论。

更新

现在(2019年)cut命令应该应用在第4个字段:cut -f4

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

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

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

pending
running
stopping
stopped
shutting-down
terminated

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

脚本假设凭据概要文件存储在文件~/中。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

您可以使用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)