There are different ways to check the instance-state
.
Use:
describe-instance-status.
You can use describe-instance-status
: The describe-instance-status
command of the AWS Command Line Interface (CLI) is used to retrieve the status of one or more Amazon Elastic Compute Cloud (EC2) instances. By default, it returns the status of running instances. To include stopped instances in the returned status information, you can use the --include-all-instances
option.
Here is an example of how you can use this option:
$ aws ec2 describe-instance-status --include-all-instances
the above command will return the status information for both running and stopped instances.
Note that you can also filter the returned results using the --filters
option. For example, you can use the following command to only retrieve the status of stopped instances:
$aws ec2 describe-instance-status --filters "Name=instance-state-code,Values=80"
Note: here 80
is the code for stopped
instances and 16
for running
ones. Below one will give you the info about stopped
instance .
$ aws ec2 describe-instance-status --include-all-instances --filters Name=instance-state-name,Values='stopped'
You can get both the running
as well as stooped
instance-state
while including the option --include-all-instances
as used in the below given example...
$ aws ec2 describe-instance-status --include-all-instances --filters Name=instance-state-name,Values='*' --query 'InstanceStatuses[*].{InstanceId: InstanceId, State: InstanceState.Name}' --profile lab--output table
------------------------------------
| DescribeInstanceStatus |
+----------------------+-----------+
| InstanceId | State |
+----------------------+-----------+
| i-0a4209dkc6549a2ea | running |
| i-09379cj420ed015f2 | running |
| i-0c9e1100de0105ed6 | stopped |
| i-0f57b147ea9124344 | running |
| i-02e4cbcbe10cb5e79 | stopped |
+----------------------+-----------+
describe-instances.
You can check the instance-state-name
regardless of the state
of the instance (pending | running | shutting-down | terminated | stopping | stopped )
while using with describe-instances
.
$ aws ec2 describe-instances --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0a4209dkc6549a2ea | t3.xlarge | running |
| i-09379cj420ed015f2 | t2.small | running |
| i-0c9e1100de0105ed6 | m5.xlarge | stopped |
| i-0f57b147ea9124344 | c6i.xlarge | running |
| i-02e4cbcbe10cb5e79 | t1.micro | stopped |
+----------------------+----------------+----------+
However, you can get a specific state
of an instance using --filers
and --query
, for example, if you are looking for only a stopped
instance then you can use like below...
$ aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0c9e1100de0105ed6 | m5.xlarge | stopped |
| i-02e4cbcbe10cb5e79 | t1.micro | stopped |
+----------------------+----------------+----------+
In a similar fashion, you can check only the running
instance …
$ aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[*].{InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile lab --output table
----------------------------------------------------
| DescribeInstances |
+----------------------+----------------+----------+
| InstanceId | InstanceType | State |
+----------------------+----------------+----------+
| i-0a4209dkc6549a2ea | t3.xlarge | running |
| i-09379cj420ed015f2 | t2.small | running |
+----------------------+----------------+----------+
Note:
Further, I personally like to dictionary format along with tabular output(--output table
) while using AWS CLI to get the values in nice tabular form which is easy for readability, hence the same here to get the values of the instance-state
in a hash form (i.e., a dictionary or associative array), you I have combined that with the --query
to get a nicer readable output.