Delete EC2 Instance

User
how to delete an instance aws
Assistant
To delete an instance in AWS, you can do it through the AWS Management Console, AWS CLI, or AWS SDKs. Here's how to delete an EC2 instance using each method:

### Method 1: AWS Management Console

1. **Log in to the AWS Management Console**
– Go to [AWS Management Console](https://aws.amazon.com/console/).
– Sign in with your credentials.

2. **Navigate to the EC2 Dashboard**
– Click on **Services** in the top-left corner.
– Search for **EC2** and select it.

3. **Locate the Instance**
– In the left sidebar, click on **Instances** under the **Instances** section.
– Find the instance you want to delete by looking at the list.

4. **Select the Instance**
– Check the checkbox next to the instance you want to delete.

5. **Terminate the Instance**
– Click the **Instance state** dropdown menu at the top.
– Select **Terminate instance**.
– Confirm the action by clicking **Terminate** in the popup dialog.

6. **Verify Termination**
– The instance state will change to **shutting-down** and then to **terminated**.

### Method 2: AWS CLI

If you have the AWS CLI installed and configured, you can delete an instance using the following command:

1. **List Instances (Optional)**

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,Tags]' --output table

2. **Terminate the Instance**

aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxx

Replace `i-xxxxxxxxxxxx` with the ID of your EC2 instance.

3. **Verify the Instance Termination**

aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxx --query 'Reservations[*].Instances[*].State.Name'

### Method 3: Using AWS SDK (e.g., Python Boto3)

If you're using Python, you can delete an instance using the Boto3 library:

1. **Install Boto3 (if not already installed)**

pip install boto3

2. **Python Script to Terminate Instance**

import boto3

   ec2 = boto3.client('ec2')
   instance_id = 'i-xxxxxxxxxxxx'

   response = ec2.terminate_instances(InstanceIds=[instance_id])
   print(f'Terminating instance {instance_id}:', response)

3. **Run the Script**

python terminate_instance.py

### Notes
– Terminated instances cannot be restarted; they are permanently deleted.
– Make sure to back up any important data before terminating an instance.
– You may want to **stop** the instance first if you're not sure about permanently deleting it.

Let me know if you need further assistance with any of these methods!

Leave a Comment

Your email address will not be published. Required fields are marked *