How to Terminate an EC2 Instance in AWS Quickly
To terminate an EC2 instance, use the AWS Management Console or AWS CLI with the
terminate-instances command specifying the instance ID. Terminating stops and deletes the instance permanently, so ensure you have backups before proceeding.Syntax
Use the AWS CLI command to terminate an EC2 instance by specifying its unique InstanceId. This command stops and deletes the instance permanently.
aws ec2 terminate-instances --instance-ids <instance-id>: Terminates the specified instance.
bash
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0Example
This example shows how to terminate an EC2 instance with ID i-0abcd1234efgh5678 using the AWS CLI. It demonstrates the command and the typical output confirming the termination.
bash
aws ec2 terminate-instances --instance-ids i-0abcd1234efgh5678Output
{
"TerminatingInstances": [
{
"InstanceId": "i-0abcd1234efgh5678",
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}
Common Pitfalls
Common mistakes when terminating EC2 instances include:
- Terminating the wrong instance due to incorrect
InstanceId. - Not backing up important data before termination, causing data loss.
- Expecting the instance to stop but not terminate; termination deletes the instance permanently.
- Trying to terminate instances that are part of an Auto Scaling group without adjusting the group settings, which may recreate the instance.
bash
Wrong way: aws ec2 terminate-instances --instance-ids i-wronginstanceid Right way: aws ec2 terminate-instances --instance-ids i-correctinstanceid
Quick Reference
Remember these key points when terminating EC2 instances:
- Use the exact
InstanceIdto avoid mistakes. - Termination is permanent; data on instance store volumes is lost.
- Check if the instance is part of an Auto Scaling group before terminating.
- Use AWS Console or CLI based on your preference.
Key Takeaways
Always verify the instance ID before terminating an EC2 instance.
Terminating an instance deletes it permanently; back up any important data first.
Use the AWS CLI command 'aws ec2 terminate-instances --instance-ids ' to terminate via command line.
Instances in Auto Scaling groups may be recreated unless the group is adjusted.
You can also terminate instances easily via the AWS Management Console.