Reputation: 11834
I am trying to get the status of few EC2 instance using AWS CLI
aws ec2 describe-instance-status --instance-ids xxxxxx yyyyyyyy zzzzzzzz
But in the output It only lists the instances which are running and not which are stopped.
How can i view the instances state even they are running or stopped
Upvotes: 5
Views: 11105
Reputation: 8826
There are different ways to check the instance-state
.
describe-instance-status.
You can use
describe-instance-status
: Thedescribe-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 asstooped
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 thestate
of the instance(pending | running | shutting-down | terminated | stopping | stopped )
while using withdescribe-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 astopped
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 |
+----------------------+----------------+----------+
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.
Upvotes: 11
Reputation: 11182
Yes, if you pass the --include-all-instances
option to the command it will also show stopped instances
From AWS documentation about the describe-instance-status command:
--include-all-instances | --no-include-all-instances (boolean)
When true , includes the health status for all instances. When false, includes the health status for running instances only.
Default: false
Upvotes: 0