On-demand EC2 instances on AWS are charged on an hourly basis, and you need to terminate or delete the EC2 instance right after you have performed your desired task on it. This guide describes all the steps which are performed to delete the EC2 instance on AWS using AWS console and command-line interface. Following are the points which will be discussed in this guide.

NOTE: Before deleting the EC2 instance, the user must have enough IAM permissions assigned; otherwise, the user will not be able to delete the EC2 instance.

Delete EC2 instance using AWS management console

This section of the guide will discuss different steps involved in deleting an EC2 instance on AWS using the AWS management console. Using the AWS management console to delete the EC2 instance is much easier than using the command-line interface for beginners.

Disable termination protection on EC2 instance

The first step to terminate or delete an EC2 instance is to disable termination protection on the instance. Termination protection is used to prevent the EC2 instance from accidental deletion. Termination protection can be set to the enabled or disabled state while launching the EC2 instance or enabled after the EC2 instance has been launched. The EC2 instance can not be terminated or deleted if the termination protection is enabled. Log into the AWS management console and go to the EC2 service.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-01.png" data-lazy- height="375" src="data:image/svg xml,” width=”618″>

Select the instances from the left side panel.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-02.png" data-lazy- height="456" src="data:image/svg xml,” width=”356″>

It will display all the instances regardless of their state. Select the instance you want to delete and click on the Actions button. It will display a list of different options. Select the instance settings options, and it will open a new list.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-03.png" data-lazy- height="706" src="data:image/svg xml,” width=”860″>

Click on the change termination protection, and it will display the status of instance termination protection. Disable the termination protection by unchecking the enable button if it is checked. Click on the save button after disabling the termination protection.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-04.png" data-lazy- height="470" src="data:image/svg xml,” width=”974″>

Delete EC2 instance

After disabling the termination protection, now it is time to delete the EC2 instance. Select the instance you want to delete from the EC2 console and click on the instance state button. A dropdown list will appear with different options.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-05.png" data-lazy- height="352" src="data:image/svg xml,” width=”456″>

Click on the Terminate option from the list, and it will ask for your consent to delete the EC2 instance.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-06.png" data-lazy- height="697" src="data:image/svg xml,” width=”943″>

Click on the Terminate button at the bottom right corner of the consent pop-up, and the instance will go to the shutting down state. After sometime, the instance state will change from shutting down to Terminated. EC2 instances do not disappear from the list of instances right after they are terminated. It will take some time to disappear terminated instances from the list of instances.

Delete EC2 instance using AWS command-line interface

This guide section describes different steps used to delete an EC2 instance on AWS using the command line interface. Deleting an instance from the command line interface is much difficult for beginners, but it can be used to automate the process using some shell or batch scripts.

Install awscli package

EC2 instances can also be terminated or deleted from your local system by using the command-line interface. For this, first, download the binaries of the awscli package using the curl command.

[email protected]:~$ curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip” -o “awscliv2.zip”

Unzip the downloaded binaries for the package.

[email protected]:~$ unzip awscliv2.zip

Install the unzipped binaries on ubuntu.

[email protected]:~$ sudo ./aws/install

After installing, check the version of the package to verify the installation.

[email protected]:~$ aws –version

Generate AWS command-line interface credentials

To generate AWS CLI credentials, go to the IAM service from the AWS management console.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-07.png" data-lazy- height="377" src="data:image/svg xml,” width=”691″>

Select the users option from the left side panel of the IAM service.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-08.png" data-lazy- height="489" src="data:image/svg xml,” width=”418″>

It will list down all the users. Click on your user account and select the credentials tab.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-09.png" data-lazy- height="293" src="data:image/svg xml,” width=”974″>

Scroll down to the Access key section and click on create access key button to generate the access key ID and secret access key.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-10.png" data-lazy- height="314" src="data:image/svg xml,” width=”743″>

Configure AWS command-line interface credentials

From your local machine, run the following command to configure the AWS access key ID and secret access key on your machine.

[email protected]:~$ aws configure

It will ask for the CLI credentials (AWS access key ID and secret access key).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-11.png" data-lazy- height="195" src="data:image/svg xml,” width=”974″>

Disable API termination protection on EC2 instance

Before terminating the EC2 instance, disable the termination protection on the EC2 instance. For this, first list all the EC2 instances using the command line interface.

[email protected]:~$ aws ec2 describe-instances –query “Reservations[].Instances[].InstanceId”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-12.png" data-lazy- height="202" src="data:image/svg xml,” width=”743″>

Copy the instance ID and run the following command to disable API termination protections.

[email protected]:~$ aws ec2 modify-instance-attribute –instance-id –disable-api-termination “{”Value”: false}”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-13.png" data-lazy- height="154" src="data:image/svg xml,” width=”974″>

Delete EC2 instance

After disabling the API termination protection, run the following command to terminate the instance specified by the EC2 instance ID.

[email protected]:~$ aws ec2 terminate-instances –instance-ids

After running the above command, it will terminate the EC2 instance and generate the following output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/delete-EC2-instance-14.png" data-lazy- height="449" src="data:image/svg xml,” width=”974″>

Conclusion

As a solutions architect on AWS, it is common to launch and terminate EC2 instances on AWS. Deleting an on-demand EC2 instance in time can save a lot more cost depending upon the type of instance. In this guide, two different ways (using AWS management console and AWS command-line interface) have been discussed to terminate or delete an EC2 instance. After going through this guide, it will be much easier for you to terminate an EC2 instance.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Zain-150×150.jpg615bbdbc07312.jpg" height="112" src="data:image/svg xml,” width=”112″>

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I’m from Gujranwala, Pakistan and currently working as a DevOps engineer.