我经常在不同地区之间切换实例,有时我忘记关闭来自其他地区的正在运行的实例。我找不到任何方法来查看亚马逊控制台上的所有正在运行的实例 有没有办法显示所有正在运行的实例而不管区域?
答案 0 :(得分:48)
编辑:确保使用Classic Tag Editor。经过重新设计的新标签编辑器不再跨区域搜索。
一个非显而易见的GUI选项是Resource Groups > Tag Editor
,然后单击classic Tag Editor链接。在这里,您可以找到所有区域中的所有实例,即使未标记该实例也是如此。
答案 1 :(得分:46)
我认为您目前无法在AWS GUI中执行此操作。但是这里有一种方法可以使用AWS CLI列出所有区域的所有实例:
<!DOCTYPE html>
<html lang="en" class="">
<head>
<meta charset="UTF-8">
<title>Document</title>
<base href="./">
<link href="styles.bundle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table id="myTable">...</table>
</body>
<script type="text/javascript" src="inline.bundle.js" ></script>
<script type="text/javascript" src="vendor.bundle.js" ></script>
<script type="text/javascript" src="main.bundle.js" ></script>
</html>
取自here(如果你想看到完整的讨论)
另外,如果你得到了
您必须指定一个区域。您还可以通过运行“aws configure”
来配置您的区域
您可以使用for region in `aws ec2 describe-regions --region us-east-1 --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region
done
这样做,感谢@Sabuncu的评论。
答案 2 :(得分:7)
每次创建资源时,都会使用名称对其进行标记,现在您可以使用资源组查找所有地区都带有名称标记的所有类型的资源。
答案 3 :(得分:7)
基于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
脚本为每个区域并行运行aws ec2 describe-instances
(现在为15!),并从json输出中仅提取相关位(状态,标记,可用区)。需要set +m
,因此后台进程在开始/结束时不会报告。
示例输出:
{
"type": "t2.micro",
"state": "stopped",
"tags": [
{
"Key": "Name",
"Value": "MyEc2WebServer"
},
],
"zone": "eu-central-1b"
}
答案 4 :(得分:7)
@imTachu解决方案效果很好。要通过AWS控制台执行此操作...
答案 5 :(得分:5)
答案 6 :(得分:4)
在控制台中
转到VPC仪表板https://console.aws.amazon.com/vpc/home
,然后单击Running instances
-> See all regions
。
在CLI中
例如,将其添加到.bashrc
。重新加载source ~/.bashrc
,然后运行
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'..
答案 7 :(得分:2)
您可以在所有地区投放DescribeInstances()
。
此外,您可以:
NodeJS中的示例:
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
函数中,DescribeInstances()
可以
调用。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函数来获取任何状态[运行,停止]的所有实例,并且从任何地区,还会提供有关实例类型和各种其他参数的详细信息。
The Script遍历所有AWS区域并调用DescribeInstances()
以获取实例。
您只需要创建一个运行时nodejs
的lambda函数。
您甚至可以创建API并在需要时使用它。
此外,您可以查看适用于DescribeInstances的AWS官方文档,以探索更多选项。
答案 8 :(得分:1)
用于打印所有区域中所有实例 ID 的快速 bash oneliner 命令:
$ 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
...
答案 9 :(得分:0)
首先转到AWS Management console并单击“资源”组:
然后找到Network and Content Delivery
并单击VPC
:
然后找到正在运行的实例并展开以查看所有区域。在这里,您可以找到所有区域的所有正在运行的实例:
答案 10 :(得分:0)
下面的我的脚本,基于本文和其他地方的各种技巧。该脚本比起长长的命令行更容易(至少对我来说)。
该脚本假定凭据配置文件存储在文件~/.aws/credentials
中,如下所示:
[default]
aws_access_key_id = ODJFDOIOIJIJFJDIOFHJ
aws_secret_access_key = spdofksdpfok345opdfkgfdpogkdsfjsd
[work]
aws_access_key_id = GF6TPDWBZMZWCZ8P1002
aws_secret_access_key = fpoksdfopdskf4350sdfsjdfkosdfjlfs
脚本:
#!/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 -f3 | 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
答案 11 :(得分:0)
我创建了一个开源脚本,该脚本可帮助您列出所有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
答案 12 :(得分:0)
CRUD AWS resources的好工具。在所有区域中找到[EC2 | RDS | IAM ..]。可以对过滤器结果进行运算(停止)。
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]
答案 13 :(得分:0)
要并行运行作业并使用多个配置文件,请使用此脚本。
#!/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
屏幕截图:
答案 14 :(得分:0)
您可以使用旨在枚举云资源(跨区域和跨帐户扫描)的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)
答案 15 :(得分:0)
我必须终止所有区域的实例,AWS客户支持与我共享了以下链接
美国东部(弗吉尼亚北部)----- https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#Instances
美国东部(俄亥俄州)------------ https://console.aws.amazon.com/ec2/v2/home?region=us-east-2#Instances
美国西部(加利福尼亚北部)---- https://console.aws.amazon.com/ec2/v2/home?region=us-west-1#Instances
美国西部(俄勒冈州)---------- https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#Instances
加拿大(中部)---------- https://console.aws.amazon.com/ec2/v2/home?region=ca-central-1#Instances
欧盟(爱尔兰)-------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-1#Instances
欧盟(法兰克福)------------ https://console.aws.amazon.com/ec2/v2/home?region=eu-central-1#Instances
欧盟(伦敦)--------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-2#Instances
欧盟(巴黎)--------------- https://console.aws.amazon.com/ec2/v2/home?region=eu-west-3#Instances
亚太地区(新加坡)-https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#Instances
亚太地区(悉尼)----- https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-2#Instances
亚太地区(首尔)------ https://console.aws.amazon.com/ec2/v2/home?region=ap-northeast-2#Instances
亚太地区(东京)------ https://console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#Instances
亚太地区(孟买)----- https://console.aws.amazon.com/ec2/v2/home?region=ap-south-1#Instances
南美(圣保罗)-https://console.aws.amazon.com/ec2/v2/home?region=sa-east-1#Instances
欧盟北部(斯德哥尔摩)-https://eu-north-1.console.aws.amazon.com/ec2/v2/home?region=eu-north-1#Instances:sort=instanceId
希望有人会觉得有用
答案 16 :(得分:0)
基于@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}"
)