在shell脚本或Python中,如何查看正在运行的EC2实例数以及每个AWS区域中的实例数?
答案 0 :(得分:2)
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --output text | wc -l
如果您希望为非默认区域执行此操作:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --region ap-southeast-2 --output text | wc -l
答案 1 :(得分:0)
我建议您先安装AWS CLI。然后看看https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html,它将向您展示如何获取正在运行的实例列表。您可以编写一个bash脚本来调用此describe-instances命令并使用可用的过滤器。
答案 2 :(得分:0)
您也可以按如下方式使用配置服务:
% aws configservice get-discovered-resource-counts --region eu-west-1 --profile MyProfile
{
"totalDiscoveredResources": 828,
"resourceCounts": [
...ommitted for brevity's sake....
{
"resourceType": "AWS::EC2::SecurityGroup",
"count": 94
},
{
"resourceType": "AWS::EC2::NetworkInterface",
"count": 60
},
{
"resourceType": "AWS::IAM::Role",
"count": 56
},
{
"resourceType": "AWS::EC2::Volume",
"count": 55
},
{
"resourceType": "AWS::EC2::Instance",
"count": 44
},
...ommitted for brevity's sake....
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configservice/index.html
答案 3 :(得分:-2)
试试这个..
aws ec2 list-instances