- Delete EC2 instance using AWS management console
- Delete EC2 instance using AWS command-line interface
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.
Select the instances from the left side panel.
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.
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.
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.
Click on the Terminate option from the list, and it will ask for your consent to delete the EC2 instance.
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.
Unzip the downloaded binaries for the package.
Install the unzipped binaries on ubuntu.
After installing, check the version of the package to verify the installation.
Generate AWS command-line interface credentials
To generate AWS CLI credentials, go to the IAM service from the AWS management console.
Select the users option from the left side panel of the IAM service.
It will list down all the users. Click on your user account and select the credentials tab.
Scroll down to the Access key section and click on create access key button to generate the access key ID and secret access key.
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.
It will ask for the CLI credentials (AWS access key ID and secret access key).
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.
Copy the instance ID and run the following command to disable API termination protections.
Delete EC2 instance
After disabling the API termination protection, run the following command to terminate the instance specified by the EC2 instance ID.
After running the above command, it will terminate the EC2 instance and generate the following output.
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.