Share with friends
Welcome to this Amaz(on)ing tutorial on how to delete EC2 instances in AWS.
Let me guess, you are trying to delete one of your EC2 instances, or stuck at some step and need help?
Kudos! You are at the right place. This is the only guide you'll need to read on how to delete EC2 instances in AWS.
In this tutorial, we are going to discuss all the possible ways to delete your EC2 instances.
But before we dive in, let's see some most common EC2 terms. If you are already an AWS pro, feel free to skip this section.
AWS EC2 - Most Common Terms
Here are some of the most common terms of AWS EC2.
-
Amazon EC2 instance: A virtual server that runs on the Amazon Elastic Compute Cloud (EC2).
-
Instance type: The type of Amazon EC2 instance, which determines the CPU, memory, and storage capacity of the instance.
-
AMI: An Amazon Machine Image (AMI) is a template that defines the software and hardware configuration of an Amazon EC2 instance.
-
EBS: Elastic Block Store (EBS) is a block-level storage service that provides persistent storage for Amazon EC2 instances.
-
VPC: A Virtual Private Cloud (VPC) is a logical network that you can create in the AWS cloud.
-
Subnet: A subnet is a range of IP addresses within a VPC.
-
Security group: A security group is a set of firewall rules that control the traffic that can flow into and out of an Amazon EC2 instance.
-
Elastic IP address: An Elastic IP address is a static IP address that can be associated with an Amazon EC2 instance.
-
Auto Scaling: Auto Scaling is a service that automatically scales your Amazon EC2 instances up or down based on demand.
Here are some additional terms that you may find handy when working with AWS EC2.
-
Spot Instances: Spot Instances are spare EC2 instances that are available at a discounted price.
-
EBS snapshots: EBS snapshots are point-in-time copies of your EBS volumes.
-
EBS volumes: EBS volumes are block-level storage devices that can be attached to Amazon EC2 instances.
-
EFS: Elastic File System (EFS) is a managed file system that you can use to store files for your Amazon EC2 instances.
-
S3: Amazon Simple Storage Service (S3) is an object storage service that you can use to store any type of data.
Also Read: Differences between S3, EFS, and EBS in AWS
Terminate Your AWS EC2 Instances
There are multiple ways in which you can do THIS, using the AWS console, or using the CLI (AWS CLI and the AWS Tools for PowerShell, for Windows).
Prerequisites
Here are a few things you need to do before you can delete the EC2 instances.
-
Install and Configure AWS CLI
-
Disable termination protection from the EC2 instance that you want to delete
Now you are good to go.
IAM permissions must be provided to the user before the EC2 instance may be deleted; if not, the user won't be allowed to do so.
Follow along to explore all the ways.
Also Read: What the Heck is Internal Developer Platform - IDP?
Deleting AWS EC2 Instances Using AWS Management Console
Amazon EC2 console is a straightforward online interface for managing Amazon EC2 resources and instances.
You may access the Amazon EC2 console if you've created an AWS account by logging into the AWS Management Console and choosing EC2 from the console's home page.
To terminate an instance in the AWS Management Console, follow these steps:
Before terminating an instance, check that there won't be a data loss of any kind. Verify that your Amazon EBS volumes won't be deleted on termination, and make sure to copy any important data from your instance store volumes to persistent storage like Amazon EBS or Amazon S3.
-
Open the Amazon EC2 console by navigating to https://console.aws.amazon.com/ec2/
-
In the navigation pane, click on "Instances" to view your instances.
-
Select the instance you want to terminate by checking the checkbox next to it.
-
From the "Instance state" drop-down menu at the top, choose "Terminate instance."
-
A confirmation prompt will appear. Review the details and if you're ready to proceed, click on "Terminate."
After confirming the termination, AWS will initiate the termination process for the selected instance.
Be aware that terminating an instance permanently deletes it, and any data stored on the instance will be lost (forever).
Therefore, it is essential to back up any necessary data before proceeding with termination.
An EC2 instance that uses an EBS (Elastic Block Storage) volume as its root device is known as an "EBS-backed" instance (as stated above). Although EBS volumes are redundant, "virtual" discs that are not bound to any specific hardware, they are only available in a specific EC2 availability zone.
How to Delete Amazon EC2 Instances Using CLI
There are 2 types of CLI methods to delete an EC2 instance, as stated below.
1. Using terminate-instances (AWS CLI)
AWS Command Line Interface lets you interact with AWS services using commands in the terminal. Supports Windows, Mac, and Linux.
There are 2 versions of AWS CLI, v1 (major), v2. This guide is based on the v2 version.
To terminate an Amazon EC2 instance, use the below command.
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Output:
{
"TerminatingInstances": [
{
"InstanceId": "i-1234567890abcdef0",
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}
Other flags you can use are:
[--dry-run | --no-dry-run], [--cli-input-json <value>], [--generate-cli-skeleton <value>], [--debug], [--endpoint-url <value>], [--no-verify-ssl], [--no-paginate], [--output <value>], [--query <value>], [--profile <value>], [--region <value>], [--version <value>], [--color <value>], [--no-sign-request], [--ca-bundle <value>], [--cli-read-timeout <value>], [--cli-connect-timeout <value>]
2. Remove-EC2Instance (AWS Tools for Windows PowerShell)
A collection of PowerShell modules based on the features made available by the AWS SDK for .NET. It calls the Amazon Elastic Compute Cloud (EC2) TerminateInstances API operation.
Syntax (with all flags):
Remove-EC2Instance
-InstanceId <Object[]>
-Select <String>
-PassThru <SwitchParameter>
-Force <SwitchParameter>
-ClientConfig <AmazonEC2Config>
Example:
Remove-EC2Instance -InstanceId i-12345678
The instance may be operating or in a "stopped" state when this example terminates it.
Before continuing, the cmdlet will ask for confirmation; use the -Force switch to ignore the request.
Supported Version: AWS Tools for PowerShell: 4.1.368.0
Delete EBS to Delete AWS EC2 Instances
EC2 Instances have storage associated in the form of EBS (Elastic Block Storage) volumes.
Even when the EC2 instances they were connected to are no longer running, unutilized (unattached) EBS Volumes can continue to incur costs.
To delete unused AWS Elastic Block Store (EBS) volumes, follow these steps:
-
Open the AWS Management Console.
-
Navigate to the EC2 service.
-
Click on "Volumes" in the left navigation pane.
-
Review the list of EBS volumes and identify the ones that are no longer in use or needed.
-
Select the unused EBS volumes by clicking the checkboxes next to them.
-
Click on the "Actions" button.
-
Choose "Delete Volumes" from the dropdown menu.
-
Confirm the deletion when prompted.
Also Read: Which one to Choose - EKS or ECS?
Termination vs Stopping an EC2 Instance
Terminating an EC2 instance means permanently shutting it down and removing it from your account. This action is permanent.
It deletes the instance and all its associated data. Once terminated, you cannot restart or recover the instance. Be mindful of it.
On the other hand, stopping an EC2 instance temporarily halts its execution and releases the underlying hardware resources, but the instance remains in your account and you still get charged for it.
When you stop an instance, its state changes to "stopped," and if you wish, you can start it again later, retaining the same instance ID, IP address (if it's an Elastic IP), and other configuration details.
The instance's EBS volumes and data are also securely preserved.
In a nutshell, to minimize any extra expenditures, you should terminate an instance unless you intend to start it soon.
Now that you know all about how you can delete/terminate your instance, let's learn about how you can protect your instances from deletion (maybe by an intern).
Also Read: The Only Docker Commands Cheat Sheet You'll Ever Need
How to Protect EC2 Instances from Termination
You have the flexibility to terminate your instance through the Amazon EC2 console, command line interface, or API by default.
However, to avoid accidental termination, you can enable termination protection for your instance.
1. DisableApiTermination
The DisableApiTermination attribute governs the ability to terminate the instance using the console, CLI, or API. By default, termination protection is not enabled for your instance.
You have the option to configure this attribute during instance launch, while the instance is running, or when it is in a stopped state (applies to Amazon EBS-backed instances).
Here are a few cases for which DisableApiTermination won't help.
-
Terminating an instance from within the operating system.
-
For spot instances
-
Termination due to hardware or software issues.
-
Auto Scaling termination policies overriding the attribute.
-
IAM permissions allow modification of the attribute.
Consider additional safeguards for instance protection, access controls, monitoring, and backups.
2. Activate AWS EC2 Termination Protection while Launching
Here are the steps to activate termination protection for an instance during its launch.
-
Access the Amazon EC2 console by visiting https://console.aws.amazon.com/ec2/.
-
On the dashboard, click on "Launch Instance" and follow the instructions provided in the wizard.
-
On the "Configure Instance Details" page, mark the checkbox labeled "Enable termination protection."
3. Activate Termination Protection for Running/Stopped Amazon EC2 Instance
Here's how to activate termination protection for a currently running or stopped instance.
-
Choose the desired instance, then click on "Actions," followed by "Instance Settings," and finally "Change Termination Protection."
-
Select "Yes" to enable termination protection.
4. Activate AWS EC2 Instance Termination Protection using Command Line
Follow the below steps to activate or deactivate termination protection using the command line.
A. Using AWS-CLI
The modify-instance-attribute modifies the instance type of the given instance. The instance must be in the stopped state.
aws ec2 modify-instance-attribute \
--instance-id i-1234567890abcdef0 \
--instance-type "{\"Value\": \"m1.small\"}"
For more information, read this doc.
B. Using AWS Tools for Windows PowerShell
To modify the instance type of the specified instance, enter the following command in PowerShell.
Edit-EC2InstanceAttribute -InstanceId i-12345678 -InstanceType m3.medium
Read this document for more information.
Also Read: How to Clean up Docker Resources?
How to Clean up Resources in AWS?
It is a good practice to clean up (delete) any resources that you no longer need. Here's a list of resources you can clean to free up resources and save AWS costs in the process.
-
Tasks
-
Services
-
Clusters
-
Container Instances
-
Elastic Load Balancing (ELB) Load Balancers
-
Auto Scaling Groups
-
Databases
1. Deleting the RDS Database
If you have an active RDS database and no longer need it, you can delete it to prevent any further charges.
To delete an RDS database using the console, follow these steps:
-
Access the Amazon RDS console at https://console.aws.amazon.com/rds/.
-
Navigate to the Databases section in the sidebar.
-
Select the RDS database created for this tutorial.
-
Click on the Actions dropdown and choose "Delete."
-
Enter "delete me" in the provided box and click on "Delete" to confirm.
2. CloudWatch Alarm Cleanup
To delete an alarm using the Amazon CloudWatch console:
-
Open the Amazon CloudWatch console at https://console.aws.amazon.com/cloudwatch/.
-
Select a region from the navigation pane.
-
Check the box next to the alarm you want to delete and click "Delete".
-
Confirm the deletion by clicking "Yes, Delete" when prompted.
3. Elastic Load Balancer Cleanup
To delete a load balancer using the AWS Management Console, you can follow these steps:
-
Access the Amazon EC2 console: Visit the URL https://console.aws.amazon.com/ec2/
-
Choose the desired region: Select the appropriate region from the available options.
-
Navigate to Load Balancers: Locate and click on "Load Balancers" to access the load balancers section.
-
Select the load balancer to delete: Click the checkbox next to it to choose it.
-
Delete the load balancer: Click on the "Delete" button after selecting the desired load balancer.
-
Confirm the deletion.
Also Read: Differences b/w Application Load Balancer and Network Load Balancer
4. Delete Key Pair
To delete a key pair using the Console, follow these steps:
-
Open the Amazon EC2 console: Go to the AWS Management Console and navigate to the EC2 service by clicking on "EC2" in the services list.
-
Select the desired region: In the top-right corner of the console, click on the region dropdown.
-
Navigate to Key Pairs: In the EC2 console's navigation pane on the left side, under the "Network & Security" section, click on "Key Pairs".
-
Select the key pair to delete: In the list of key pairs, locate the one you want to delete.
-
Delete the key pair: With the key pair selected, click on the "Actions" button above the list of key pairs, and then click on "Delete" from the dropdown menu.
-
Confirm the deletion.
This deletes only the Amazon EC2's copy of the public key.
If you get a notification that you are running out of space in your EC2 instance, you can run the command df -h to check for all the files and file types occupying the space.
You can run du -h in ~ (root) to see the list of all directories on the server.
Frequently Asked Questions
Can I Restart a Terminated Instance?
No, once an instance is terminated, it cannot be restarted. Terminating an instance permanently shuts it down and removes it from your account.
The termination process involves the permanent deletion of the instance and its associated data.
But, if you really need to restore the functionality of a terminated instance, you might consider launching a new instance with similar configurations and settings as the terminated one.
How do I know if my EC2 instance has been terminated successfully?
You can check the AWS Cost and Usage Report or AWS Cost Explorer to make sure that the resources are no longer generating charges. Don't worry; you are not billed by AWS for any instances that are not in the running state.
How to Delete Terminated EC2 Instances in AWS?
Terminated instances will go away in a couple of hours. There is nothing you can do to manually remove them. Don't worry; you are not billed by AWS for any instances that are not in the running state.
Share with friends