Stopping, Hibernating and Recovering EC2 Instances
This lesson explains how stopping, resuming and recovering an instance from hardware failure decide what happens to its data, addresses and in-memory state.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish reboot, stop, hibernate and terminate.
- Explain how hibernation preserves memory.
- Explain how instances recover automatically from hardware failure.
- Choose between user data and custom AMIs for bootstrapping.
Stop, hibernate and terminate
Each instance state change affects storage, memory and addresses differently. A reboot restarts the operating system on the same host, so everything is kept except the contents of memory. Stopping an EBS-backed instance shuts it down and releases its host: EBS volumes are kept and no compute is charged, but data on instance store volumes, which are physically attached to the host, is lost, memory is lost, and the public IPv4 address is released.
Hibernating an instance saves the contents of memory to the encrypted EBS root volume before stopping, and restores it on start, so applications resume where they left off, with caches already loaded, instead of starting from scratch. Hibernation must be enabled when the instance is launched. Terminating an instance deletes it. The root volume is deleted by default, while additional EBS volumes are kept unless set to delete on termination. The following table summarises the effects.
| Action | EBS volumes | Instance store | RAM | Public IPv4 |
|---|---|---|---|---|
| Reboot | Kept | Kept | Lost | Kept |
| Stop | Kept | Lost | Lost | Released |
| Hibernate | Kept (RAM saved to the root volume) | Lost | Restored on start | Released |
| Terminate | Root deleted by default; data volumes kept unless set otherwise | Lost | Lost | Released |
Kept or lost?
Hibernation
Hibernation preserves an instance's memory while it is stopped. When an instance hibernates, the contents of its RAM are saved to its encrypted EBS root volume, which must be large enough to hold them; when it starts again, the memory is restored and running processes resume where they left off.
This suits applications that take a long time to initialise or that rely on warm caches, which would otherwise be rebuilt after every start. Hibernation is supported for instances with up to 150 GB of RAM, for up to 60 days, and it must be enabled when the instance is launched.
150GB
Maximum instance memory for hibernation
Recovering from hardware failure
When the hardware under an instance fails, the instance can recover in several ways. Simplified automatic recovery restarts an impaired instance on new hardware, keeping its instance ID, private IP addresses, Elastic IP addresses and EBS volumes; a CloudWatch alarm with the recover action does the same explicitly. This suits single instances that cannot easily be replaced.
For stateless fleets, an Auto Scaling group that terminates unhealthy instances and launches replacements is preferred, because it also handles software failures and the loss of an Availability Zone. In either case, data on instance store volumes is lost when the hardware fails, so it must be replicated elsewhere.
Bootstrapping and images
There are two ways to prepare an instance for its job, and they trade flexibility against launch time. User data is a script that runs as root when an instance first boots, installing and configuring software. It keeps the image generic, but every launch repeats the work, which slows scale-out and can fail if a download is unavailable.
An AMI captures a fully configured instance, so new instances start ready to work. AMIs are Regional, and can be copied to other Regions, for disaster recovery, and shared with other accounts. EC2 Image Builder automates building, testing and distributing such images, so they stay patched without manual work. Many designs combine the two: a prepared AMI for the slow, common steps and a short user data script for instance-specific settings. The following table summarises the features.
| Feature | What it does |
|---|---|
| User data | Runs bootstrap scripts at first launch |
| AMIs | Capture a configured instance; Regional, copied across Regions and shared across accounts |
| EC2 Image Builder | Builds, tests and distributes patched AMIs automatically |
Faster, safer launches
An analytics server with a slow start
An analytics server loads 40 GB of reference data into memory, which takes 20 minutes. It is needed only during office hours, and must recover by itself if its host fails.
| Requirement | Feature |
|---|---|
| Resume quickly each morning with memory intact | Hibernation, with an encrypted EBS root volume larger than the RAM |
| Stop paying for compute overnight | Hibernate in the evening; only EBS is charged |
| Recover from a host failure keeping its IPs | Simplified automatic recovery |
Instance store would not suit the reference data, because it is lost on hibernation and stop.
What happens to instance store data when an instance is stopped?
Show answer
It is lost. Instance store data survives a reboot but not a stop, hibernation or termination.
Exam Tips
Remember for the exam
- For a fast resume with in-memory state, use hibernation.
- When an instance must keep its IP address after a hardware failure, use EC2 automatic recovery.
- Instance store data is lost on stop, hibernation, termination or hardware failure.
- To keep the root volume after termination, turn off DeleteOnTermination.
- To produce patched AMIs automatically, use EC2 Image Builder.