[英]AWS EC2 CLI -> How to get list of avaliable instances
美好的一天,教授。 我對aws cli框架有點新意。 我需要通過控制台獲得的是指定區域中可用實例的列表,其中包含該類型的限制數。
例如,命令應如下所示:
aws ec2 describe-available-instances --region us-west-1 --type [t1.micro, c3.4xlarge, etc. If not set - list all types]
輸出看起來像:
t1.micro 5
c3.4xlarge 10
m4.4x 20
您可以使用AWS CLI和JQ將此輸出實現為JSON。
1)調用describe-instances命令。 這將檢索有關EC2實例的所有類型的元數據,包括實例類型。 我們最終將輸出過濾到Instance Type。
aws ec2 describe-instances
2)通過--filter
param指定實例類型的過濾器。 如果未指定,則此查詢將顯示count> 0的所有實例類型。這不包括0計數類型,因為輸出源自EC2實例的集合。
--filters "Name=instance-type,Values=t2.micro,t2.small"
3)通過--region
param指定一個區域。 如果未指定,AWS CLI將嘗試使用您的默認區域。
--region us-east-1
4)指定您的查詢。 輸出鍵/值對的數組,其中Key =“InstanceType”,Value = InstanceType。
--query "Reservations[].Instances[].{InstanceType:InstanceType}"
5)使用jq按實例類型分組,以便聚合like-InstanceTypes。
| jq "group_by(.InstanceType)
6)映射鍵/值對的最終數組,其中key = InstanceType和value = jq length
,或者換句話說,每組實例類型的總和。
| map({(.[0].InstanceType):length})
aws ec2 describe-instances --region us-east-1 --filters "Name=instance-type,Values=t2.micro,t2.small" --query "Reservations[].Instances[].{InstanceType:InstanceType}" | jq "group_by(.InstanceType) | map({(.[0].InstanceType):length})"
[
{
"t2.micro": 12
},
{
"t2.small": 2
}
]
如果您嘗試檢索每個實例類型的Amazon GameLift限制,請使用describe-ec2-instance-limits:
aws gamelift describe-ec2-instance-limits --query 'EC2InstanceLimits[].{EC2InstanceType:EC2InstanceType,InstanceLimit:InstanceLimit}' --region us-east-1 --output text
有目的地查看沒有過濾器的所有實例 - 使用此:
aws ec2 describe-instances --output text \
--query 'Reservations[*].Instances[*].[InstanceType]' | sort | uniq -c
輸出將如下所示:
1 m3.medium
1 m4.10xlarge
9 m4.xlarge
6 t2.large
5 t2.medium
4 t2.micro
2 t2.xlarge
並且目的是按特定類型過濾 - 只需添加過濾器,如下所示:
--filters "Name=instance-type,Values=t2.micro,t2.small"
,你的命令將如下所示:
aws ec2 describe-instances --output text \
--filters "Name=instance-type,Values=t2.micro,t2.small" \
--query 'Reservations[*].Instances[*].[InstanceType]' | sort | uniq -c
有如上所述的類似要求,但也希望過濾運行的 實例並查看每個區域的結果(已排序 )。
利用@ Vladimir-Kovpak和@ Anthony Neace的一些精彩回應,我最終得到了這個,可能會幫助某人(那些家伙幫我)
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName | sort(@)' --output text); \
do echo $region; \
aws ec2 describe-instances \
--region "${region}" \
--filter Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].{InstanceType:InstanceType}' \
| jq -r '.[].InstanceType' \
| sort \
| uniq -c \
| sort -r;
done
...導致輸出類似於以下...
ap-northeast-1
ap-northeast-2
ap-south-1
ap-southeast-1
5 m3.medium
4 m4.xlarge
3 t2.micro
2 t2.large
2 t2.medium
2 m3.large
1 t2.xlarge
1 t2.small
1 m4.large
ap-southeast-2
5 t2.medium
4 m4.xlarge
3 t2.micro
2 t2.large
2 m3.medium
2 m3.large
1 t2.xlarge
1 t2.small
1 m4.large
ca-central-1
eu-central-1
eu-west-1
eu-west-2
eu-west-3
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2
另外,為了節省每次都要粘貼它,我將它添加到我的aws別名文件中。 在cli上運行:
aws all
以下示例。 (在$ HOME / .aws / cli / alias創建)有關詳細信息,請參閱aws cli別名github repo(以及節省別名的更多時間)
# Add to your existing, or create this file at $HOME/.aws/cli/alias
[toplevel]
# Run, with 'aws who'
who = sts get-caller-identity
# Run, with 'aws all'
all = !
! g() {
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName | sort(@)' --output text); \
do echo $region; \
aws ec2 describe-instances \
--region "${region}" \
--filter Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].{InstanceType:InstanceType}' \
| jq -r '.[].InstanceType' \
| sort \
| uniq -c \
| sort -r;
done
}; g
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.