AWS EC2: Stop and Start Instances
Stop and start EC2 instances using aws ec2 stop-instances and start-instances. Save costs by stopping unused instances.
Detailed Explanation
Stopping and Starting EC2 Instances
Stopping instances pauses billing for compute charges (you still pay for EBS volumes and Elastic IPs). Starting resumes the instance, potentially on different underlying hardware.
Stop an Instance
aws ec2 stop-instances --instance-ids i-0abc123def456789
Stop Multiple Instances
aws ec2 stop-instances --instance-ids i-0abc123def456789 i-0def789abc123456
Force Stop (ungraceful shutdown)
aws ec2 stop-instances --instance-ids i-0abc123def456789 --force
Use --force only when the instance is unresponsive. It is equivalent to pulling the power cord — the OS does not get a chance to flush buffers or shut down cleanly.
Start a Stopped Instance
aws ec2 start-instances --instance-ids i-0abc123def456789
Wait for State Change
aws ec2 stop-instances --instance-ids i-0abc123def456789
aws ec2 wait instance-stopped --instance-ids i-0abc123def456789
echo "Instance is now stopped"
The wait command polls the instance state and blocks until it reaches the target state (or times out after 40 attempts, 15 seconds apart).
Important Notes
| Aspect | Stop | Start |
|---|---|---|
| Public IP | Released (changes on restart) | New IP assigned |
| Elastic IP | Retained (still charged) | Same IP |
| Instance Store | Data lost | Empty |
| EBS Volumes | Preserved | Same data |
| Private IP | Preserved | Same IP |
Cost Optimization Script
# Stop all development instances at night
aws ec2 describe-instances \
--filters "Name=tag:Environment,Values=dev" "Name=instance-state-name,Values=running" \
--query "Reservations[].Instances[].InstanceId" \
--output text | xargs aws ec2 stop-instances --instance-ids
Use Case
Reducing AWS costs by stopping development or staging instances outside business hours, performing maintenance restarts, or implementing automated start/stop schedules.