0
0
AwsHow-ToBeginner · 3 min read

How to Stop an EC2 Instance in AWS: Simple Steps

To stop an EC2 instance, use the StopInstances action in the AWS CLI or the AWS Management Console. This halts the instance without deleting it, allowing you to restart it later.
📐

Syntax

Use the AWS CLI command aws ec2 stop-instances followed by the --instance-ids option to specify which instance to stop.

This command sends a stop signal to the instance, which then shuts down safely.

bash
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
💻

Example

This example shows how to stop an EC2 instance using the AWS CLI. Replace i-1234567890abcdef0 with your instance ID.

bash
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
Output
{ "StoppingInstances": [ { "InstanceId": "i-1234567890abcdef0", "CurrentState": { "Code": 64, "Name": "stopping" }, "PreviousState": { "Code": 16, "Name": "running" } } ] }
⚠️

Common Pitfalls

  • Trying to stop an instance that is already stopped will not cause an error but does nothing.
  • Instances with instance store volumes lose data on stop; only EBS-backed instances preserve data.
  • Stopping an instance does not release its Elastic IP unless configured.
bash
aws ec2 stop-instances --instance-ids i-00000000000000000
# No error but no effect if instance is already stopped

# Correct way: Check instance state before stopping
aws ec2 describe-instances --instance-ids i-00000000000000000 --query 'Reservations[].Instances[].State.Name' --output text
# Proceed to stop only if state is 'running'
📊

Quick Reference

Remember these key points when stopping EC2 instances:

  • Use stop-instances with instance IDs.
  • Only EBS-backed instances keep data after stop.
  • Stopped instances do not incur compute charges but storage charges apply.
  • Use AWS Console or CLI based on your preference.

Key Takeaways

Use the AWS CLI command aws ec2 stop-instances --instance-ids to stop EC2 instances safely.
Stopping an instance halts it but preserves its EBS volumes and data.
Check the instance state before stopping to avoid unnecessary commands.
Stopped instances do not incur compute charges but storage costs remain.
Elastic IPs remain attached unless manually released after stopping.