Reputation: 139
Good day time, profs. I am a bit new to aws cli framework. What i need to get via console is a list of available instances in a specified region with a limit number of that type.
For example, command should look like:
aws ec2 describe-available-instances --region us-west-1 --type [t1.micro, c3.4xlarge, etc. If not set - list all types]
and the output will look like:
t1.micro 5
c3.4xlarge 10
m4.4x 20
Upvotes: 10
Views: 22072
Reputation: 6925
Had a similar requirement as above, but also wanted to filter on running instances and view the results per region (sorted).
Using some of the awesome responses from @Vladimir-Kovpak, and @ Anthony Neace, I ended up with this, may help someone (those guys helped me)
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
... resulting in output similar to below...
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
Also, to save having to paste this in each time, I added it to my aws alias file. Run on the cli with:
aws all
Example below. (create at $HOME/.aws/cli/alias) See the aws cli alias github repo for details (and more great time saving aliases)
# 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
Upvotes: 2
Reputation: 17091
With purpose see all instances without filters - use this:
aws ec2 describe-instances --output text \
--query 'Reservations[*].Instances[*].[InstanceType]' | sort | uniq -c
Output will look like this:
1 m3.medium
1 m4.10xlarge
9 m4.xlarge
6 t2.large
5 t2.medium
4 t2.micro
2 t2.xlarge
And with purpose to filter by specific type - just add filter, like this: --filters "Name=instance-type,Values=t2.micro,t2.small"
, your comand will look like this:
aws ec2 describe-instances --output text \
--filters "Name=instance-type,Values=t2.micro,t2.small" \
--query 'Reservations[*].Instances[*].[InstanceType]' | sort | uniq -c
Upvotes: 9
Reputation: 26031
You can achieve this output as JSON with AWS CLI and JQ.
1) Call the describe-instances command. This retrieves all sorts of metadata about an EC2 Instance, including the Instance Type. We will eventually filter our output down to Instance Type alone.
aws ec2 describe-instances
2) Specify an instance-type filter via the --filter
param. If this is not specified, this query will display all instance types where count > 0. This will not include 0-count types because the output is derived from your collection of EC2 instances.
--filters "Name=instance-type,Values=t2.micro,t2.small"
3) Specify a region via the --region
param. If this is not specified, AWS CLI will attempt to use your default region.
--region us-east-1
4) Specify your query. Output an array of key/value pairs where Key = "InstanceType", Value = InstanceType.
--query "Reservations[].Instances[].{InstanceType:InstanceType}"
5) Use jq to group by Instance Type, so that like-InstanceTypes will be aggregated.
| jq "group_by(.InstanceType)
6) Map a final array of key/value pairs, where key = InstanceType and value = jq length
, or in other words the sum of each group of instance types.
| 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
}
]
If you're trying to retrieve the Amazon GameLift per-instance-type limits, use describe-ec2-instance-limits:
aws gamelift describe-ec2-instance-limits --query 'EC2InstanceLimits[].{EC2InstanceType:EC2InstanceType,InstanceLimit:InstanceLimit}' --region us-east-1 --output text
Upvotes: 14