Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS EC2 CLI -> How to get list of avaliable instances

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


like image 653
simple3ant Avatar asked May 31 '17 03:05

simple3ant


People also ask

How do I get a list of EC2 Instances?

Go to VPC dashboard https://console.aws.amazon.com/vpc/home and click on Running instances -> See all regions . Example output: $ 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'..

How do I check EC2 availability?

Open the Amazon Elastic Cloud Compute (Amazon EC2) console. From the navigation bar, view the options in the Region selector. On the navigation pane, choose EC2 Dashboard. In the Service Health section, view the list of Availability Zones under Availability Zone Status.


2 Answers

You can achieve this output as JSON with AWS CLI and JQ.

Step by step:

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})

Full Example:

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})"

Output:

[
  {
    "t2.micro": 12
  },
  {
    "t2.small": 2
  }
]

Additional Notes

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

Further Reading

  • AWS Documentation - aws gamelift describe-ec2-instance-limits
  • AWS Documentation - aws ec2 describe-instances
  • AWS Documentation - Controlling Command Output from the AWS Command Line Interface
  • JQ - Builtin operators and functions
like image 102
Anthony Neace Avatar answered Nov 06 '22 22:11

Anthony Neace


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
like image 40
cn007b Avatar answered Nov 06 '22 23:11

cn007b