Sub topic 9
Instance states: stop, start, reboot, hibernate, terminate
This chapter assumes you can launch an instance with the AWS CLI and know the difference between EBS and instance store. It covers what each state change does to your data, your bill, and your public IP address.
The states
An instance moves through a small set of states. Each state change is triggered by an API call or by AWS.
What each action does
| Action | Instance ID | Memory (RAM) | EBS root volume | Instance store | Auto-assigned public IPv4 | Compute billing |
|---|---|---|---|---|---|---|
| Reboot | Kept | Cleared | Kept | Kept | Kept | Continues |
| Stop | Kept | Cleared | Kept | Lost | Released | Stops |
| Hibernate | Kept | Saved to the root volume | Kept | Lost | Released | Stops |
| Terminate | Gone | Gone | Deleted by default | Lost | Released | Stops |
Storage keeps costing money while an instance is stopped. EBS volumes and any Elastic IP address are still billed.
Observing a stop and start
Run this to list an instance, stop it, and start it again. Wait for the state to reach stopped before you start it.
aws ec2 describe-instances \
--filters Name=tag:Name,Values=tutorial-web \
--query 'Reservations[].Instances[].[InstanceId,State.Name,PublicIpAddress]' \
--output table
aws ec2 stop-instances --instance-ids i-0123456789abcdef0
aws ec2 start-instances --instance-ids i-0123456789abcdef0Run the describe-instances command again after the start. The public IP address is usually different, because AWS assigns a new one from its pool each time. If you need an address that survives a stop, use an Elastic IP, covered in a later chapter.
Reboot, stop, and hibernate
A reboot is the equivalent of restarting an operating system. The instance stays on the same physical host, so the public IP and instance store data survive. Use aws ec2 reboot-instances.
A stop shuts the operating system down. The instance can move to different hardware on the next start, which is why instance store data is lost. You can change the instance type of a stopped instance, which is the usual way to resize it.
Hibernate writes the contents of RAM to the encrypted root EBS volume and then stops. On start, the OS restores memory and your processes resume. Hibernation must be enabled at launch (--hibernation-options Configured=true), the root volume must be encrypted, and it is supported only on certain instance types and AMIs. Trigger it with aws ec2 stop-instances --hibernate.
Terminate is permanent
Termination cannot be undone. The instance is deleted, and its root volume is deleted unless DeleteOnTermination is set to false. Additional EBS volumes you attached are kept by default. A terminated instance stays visible in listings for a short time, then disappears.
To guard against mistakes, enable termination protection with aws ec2 modify-instance-attribute --instance-id i-0123456789abcdef0 --disable-api-termination. API calls to terminate will then fail until you turn it off.
Common errors
- Expecting data in instance store to survive a stop. It does not.
- Hard-coding an auto-assigned public IP in DNS or a firewall rule. It changes after a stop and start.
- Calling
stop-instances --hibernateon an instance that was not launched with hibernation enabled. The call fails. - Starting an instance immediately after calling stop. It must be fully
stoppedfirst.
Exercise
You stop an instance that has an EBS root volume, an instance store volume, and an auto-assigned public IPv4 address, then start it again. Which of the three are preserved?
Show answer
Only the EBS root volume is preserved. The instance store data is lost because the instance may start on different hardware, and the auto-assigned public IPv4 address is released and replaced with a new one.