EC2
AWS
cloud computing
instance management
tutorial

How to delete instance in EC2?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Deleting an instance in Amazon EC2 (Elastic Compute Cloud) is a common task once a virtual machine is no longer needed or its services have been transferred or upgraded. Properly terminating instances ensures that you are not incurring unnecessary costs as AWS charges based on the time duration and resources consumed by your running instances. Below is a detailed guide on how to delete an instance in EC2, complete with technical insights and examples.

Pre-requisites

Before deleting an instance, ensure that:

  • Any critical data on the instance is backed up.
  • You have appropriate permissions in AWS IAM to delete the instance.
  • The termination protection on the instance is disabled.

Steps to Delete an EC2 Instance

Step 1: Login to AWS Management Console

Navigate to the AWS Management Console and search for "EC2" in the services search bar to open the EC2 Dashboard.

Step 2: Select Instances

In the EC2 Dashboard, click on "Instances" in the left-hand navigation pane to view all your EC2 instances.

Step 3: Choose Instance to Delete

From the list of running instances, select the instance that you want to terminate by checking the checkbox next to it.

Step 4: Disable Termination Protection (if enabled)

Before terminating, ensure that termination protection is not enabled for the instance. You can do this by:

  • Selecting "Actions" > "Instance Settings" > "Change Termination Protection".
  • Uncheck the "Enable" option if it is checked.

Step 5: Terminate the Instance

With the instance selected, go to:

  • "Actions" > "Instance State" > "Terminate Instance".
  • Confirm the termination when prompted. AWS will then proceed to terminate the selected instance.

Step 6: Verify Instance Termination

You can verify the termination by:

  • Observing the instance state change from "Running" to "Shutting-down" and finally to "Terminated".

Below is a table summarizing the steps involved:

StepActionDetails
1LoginAccess AWS Management Console.
2NavigateGo to EC2 Dashboard > Instances.
3SelectChoose the instance(s) to terminate.
4Disable Termination ProtectionEnsure protection settings are disabled.
5TerminateUse instance actions to confirm termination.
6VerifyCheck instance state to ensure termination.

Additional Considerations

Data Preservation

Data stored on instance store volumes is lost when the instance is terminated. Ensure you have copied any critical data to an external location or use AWS Elastic Block Store (EBS) volumes with snapshots.

Billing Implications

Billing for EC2 instances stops once the instance is terminated. However, any associated resources (like Elastic IPs, reserved snapshots, etc.) may continue to incur charges.

Clean-up Resources

After an instance is terminated, remember to check and clean up associated AWS resources such as Security Groups, Elastic Load Balancers, and EBS volumes if no longer needed.

Scripting Instance Termination

AWS EC2 instances can also be terminated programmatically using AWS CLI or SDKs in supported languages (e.g., Python with Boto3).

Here's an example using the AWS CLI:

bash
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

Or using Python and Boto3:

python
1import boto3
2
3ec2 = boto3.client('ec2')
4
5response = ec2.terminate_instances(
6    InstanceIds=['i-1234567890abcdef0']
7)
8
9print(response)

Both examples assume you have configured your AWS credentials and have the necessary permissions to execute these commands.

By following these steps and recommendations, you can effectively and efficiently terminate EC2 instances while managing costs and preserving essential data.


Course illustration
Course illustration

All Rights Reserved.