Home ยป How to start, stop or terminate instances using AWS CLI

How to start, stop or terminate instances using AWS CLI

In a previous post, I have discussed how to create an AWS EC2 instance using the AWS CLI.

Just like creating new instances using the AWS CLI, one can also stop and start instances using the start-instances and stop-instances ZCLI commands. The command that we used for creating instances was create-instances.  However, once you have created an instance, you can stop it to avoid being charged when the instance is not in use. Later, you can start it back again to put it back in use.

Now, suppose you created an AWS EC2 instance whose id is i-0c6d777c72ce95927 and you need to stop this instance. The only thing you will need to stop an instance which is in running state is the instance id.

Here, we have the instance id as : i-0c6d777c72ce95927

To stop this instance, we will run the following command from the AWS CLI:

$ aws ec2 stop-instances –instance-ids i-0c6d777c72ce95927

The resulting output will look something like the following:

{

“StoppingInstances”: [

     {

         “CurrentState”: {

             “Code”: 64,

             “Name”: “stopping”

         },

         “InstanceId”: “i-0c6d777c72ce95927”,

         “PreviousState”: {

             “Code”: 16,      < — Instance state code

             “Name”: “running”  < — Instance State

         }

     }

]

}

Note: There are six codes related to the various instance states which are as follows:

0: pending

16: running

32: shutting-down

48: terminated

64: stopping

80: stopped

As you can see in the above output, there is a code provided for the instance state. When, you stopped the instance, it was in running state or code 16 and soon, the code will change to 64 and then 80, when the instance has fully stopped.

To start the instance again, you need to run the following command from the AWS CLI:

$ aws ec2 start-instances –instance-ids i-0c6d777c72ce95927

The output will look like the following:

{

“StartingInstances”: [

     {

         “CurrentState”: {

             “Code”: 0,

             “Name”: “pending”

         },

         “InstanceId”: “i-0c6d777c72ce95927”,

         “PreviousState”: {

             “Code”: 80,

             “Name”: “stopped”

         }

     }

]

}

When an instance starts, it is in the pending state initially. When you freshly create an instance or start an EC2 instance that you have previously stopped, its state is pending.

Now, we will try to stop and start two instances using the same command. We have two instances here that are in the stopped state and we will first start and then stop them using the same command (stop or start more than one instances or a group of instances simultaneously):

$ aws ec2 start-instances –instance-ids i-0e13c9d858a90fb9c i-07d3c83e849cb5c40

This command started both instances at once. The output looks like the following:

{

“StartingInstances”: [

     {

         “CurrentState”: {

             “Code”: 0,

             “Name”: “pending”

         },

         “InstanceId”: “i-0e13c9d858a90fb9c”,

         “PreviousState”: {

             “Code”: 80,

             “Name”: “stopped”

         }

     },

     {

         “CurrentState”: {

             “Code”: 0,

             “Name”: “pending”

         },

         “InstanceId”: “i-07d3c83e849cb5c40”,

         “PreviousState”: {

             “Code”: 80,

             “Name”: “stopped”

         }

     }

]

}

Now, that we have started more than one instance using the same command and at one go, we will check if we can stop more than one instances simultaneously, using the stop instances command. These instances are in the running state now.

$ aws ec2 stop-instances –instance-ids i-0e13c9d858a90fb9c i-07d3c83e849cb5c40

The resulting output shows that both the instances are in stopping state:

{

“StoppingInstances”: [

     {

         “CurrentState”: {

             “Code”: 64,

             “Name”: “stopping”  < — Instance State Stopping

         },

         “InstanceId”: “i-0e13c9d858a90fb9c”, < — Instance ID

         “PreviousState”: {

             “Code”: 16,

             “Name”: “running”

         }

     },

     {

         “CurrentState”: {

             “Code”: 64,            < — Instance State Code

             “Name”: “stopping”  < —- Instance State

         },

         “InstanceId”: “i-07d3c83e849cb5c40”,   < — Instance ID

         “PreviousState”: {

             “Code”: 16,

            “Name”: “running”

         }

     }

]

}

Any instance that you want to remove, must be stopped to be deleted. For example, we have just stopped the above two instances and now we can try removing them. The command used for removing instances is terminate-instances which shuts down a specified instance.

For example, we are trying to delete the two instances that we stopped in the previous step.

$ aws ec2 terminate-instances –instance-ids i-0e13c9d858a90fb9c i-07d3c83e849cb5c40

Using the above command, I have terminated two instances simultaneously which shows in the below output.

{

“TerminatingInstances”: [

     {

         “CurrentState”: {

             “Code”: 48,

             “Name”: “terminated”

         },

         “InstanceId”: “i-0e13c9d858a90fb9c”,

         “PreviousState”: {

             “Code”: 80,

             “Name”: “stopped”

         }

     },

     {

         “CurrentState”: {

             “Code”: 48,

             “Name”: “terminated”

         },

         “InstanceId”: “i-07d3c83e849cb5c40”,

         “PreviousState”: {

             “Code”: 80,

             “Name”: “stopped”

         }

     }

]

}

You can terminate instances across multiple availability zones, given termination protection is not on for any instance or the operation fails. Suppose, you try to terminate 4 instance, a pair each from two different availability zones, with one having termination protection on, you will not be able to terminate all at once. Only the instances in the zone where none of the instances has termination protection on will be terminated. The zone in which any instance has termination protection on will report failure or none of the instances in that zone will be terminated.