Sub topic 9
Instance states: stop, start, reboot, hibernate, terminate
This chapter assumes you can launch an instance with the AWS CLI (covered in the previous chapter), that you know what an EBS volume is, and that you understand public and private IPv4 addresses. You will learn what each lifecycle state means, what each state costs, and what happens to your data and your public IP address when an instance changes state.
The lifecycle at a glance
An EC2 instance is always in one of a few states. You move it between states with API calls, and EC2 sometimes moves it for you, for example when the underlying hardware fails.
The states you will see in describe-instances output are:
pending: EC2 is placing the instance on a host and preparing it. No instance usage is billed yet.running: the instance is up. Billing for instance usage applies.stoppingandstopped: the instance is shutting down or shut down. You can start it again later.shutting-downandterminated: the instance is being deleted or is deleted. It cannot be recovered.
Running, and what you pay for
While an instance is running, you pay for the instance type per second, with a 60-second minimum on Linux instances. Charges for other resources continue in every state that keeps them. An EBS volume attached to a stopped instance still costs money, because the volume still exists and holds your data.
| State | Instance usage billed | EBS volumes billed | Data on root EBS volume |
|---|---|---|---|
| pending | No | Yes | Kept |
| running | Yes | Yes | Kept |
| stopped | No | Yes | Kept |
| stopped (hibernated) | No | Yes | Kept, plus RAM contents saved |
| terminated | No | Only volumes that are not deleted on termination | Deleted by default |
Reboot
A reboot restarts the operating system, much like running reboot inside the instance. The instance stays on the same physical host, and it keeps its private IP address, public IP address, instance store data, and EBS data. The instance remains in the running state throughout.
aws ec2 reboot-instances --instance-ids i-0123456789abcdef0Use a reboot after kernel updates or when a process is stuck. Do not expect a reboot to fix a failed host or a failed system status check. For that you need a stop and start, covered next.
Stop and start
A stop shuts down the operating system and releases the instance from its host. Only EBS-backed instances can be stopped. When you start the instance again, EC2 usually places it on a different physical host.
Three things change across a stop and start:
- Instance store data is lost. Instance store volumes are physically attached to the host, so they do not survive the move.
- RAM contents are lost. Running processes end, and the instance boots fresh.
- An automatically assigned public IPv4 address is released. The instance usually gets a new one on start. The private IPv4 address stays the same.
You can observe the public IP change with this sequence:
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 instance is back in running. The public IP will most likely differ. If anything depends on that address, such as a DNS record or a firewall rule on another system, use an Elastic IP (a later chapter) or a DNS name instead.
A stop can take a minute or more. Because commands return before the state change finishes, wait for the target state in scripts:
aws ec2 wait instance-stopped --instance-ids i-0123456789abcdef0
aws ec2 wait instance-running --instance-ids i-0123456789abcdef0Stopping is also the way to change an instance's type. You stop the instance, run modify-instance-attribute with the new type, and start it again.
Hibernate
Hibernation saves the contents of RAM to the root EBS volume, then stops the instance. When you start it, the operating system reads that saved memory back, and your processes resume where they left off. This is useful for applications with long warm-up times.
Hibernation must be enabled when you launch the instance. You cannot turn it on afterward. The requirements include:
- A supported instance type and a supported AMI.
- An encrypted root EBS volume that is large enough to hold the RAM contents plus the operating system and data.
- A RAM size within the documented limit for hibernation.
You enable it at launch with --hibernation-options Configured=true, and you hibernate later with the stop command:
aws ec2 stop-instances \
--instance-ids i-0123456789abcdef0 \
--hibernateWhile hibernated, the instance is in the stopped state, so you pay for EBS storage (including the space holding the RAM image) but not for instance usage. Instance store data is still lost, and an automatically assigned public IPv4 address is still released. Check the current EC2 documentation for the supported types, AMIs, and RAM limits before relying on hibernation.
Terminate
Termination permanently deletes the instance. After a short time it remains visible as terminated, then disappears from listings. You cannot start it again.
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0What happens to storage depends on the DeleteOnTermination flag on each volume. By default, the root volume is deleted and additional volumes you attach are kept. A kept volume still costs money until you delete it. You can inspect the flag with describe-instances and change it with modify-instance-attribute.
Two protections reduce the risk of accidents:
- Termination protection (
DisableApiTermination) makes API termination calls fail until you turn the protection off. It does not stop an operating system shutdown if the shutdown behavior is set to terminate. - Instance-initiated shutdown behavior controls what happens when you run
shutdowninside the instance. The default isstop. Set it toterminateonly when you want that outcome.
Termination also does not release an Elastic IP address. That address stays allocated to your account, and AWS charges for public IPv4 addresses, so release it explicitly:
aws ec2 release-address --allocation-id eipalloc-0123456789abcdef0Choosing the right action
| Goal | Action | What you keep | What you lose |
|---|---|---|---|
| Apply an OS update | Reboot | Everything | Nothing |
| Pause cost overnight | Stop | EBS data, private IP | RAM, instance store, auto-assigned public IP |
| Resume work with processes intact | Hibernate | EBS data, RAM state | Instance store, auto-assigned public IP |
| Recover from a host failure | Stop, then start | EBS data | RAM, instance store |
| Finish with the server | Terminate | Only volumes not deleted on termination | Instance, root volume by default |
Common errors
- Assuming a stopped instance is free. The EBS volumes still bill.
- Storing data on instance store and then stopping. The data is gone on stop, hibernate, and terminate.
- Expecting the public IP to survive a stop. Only an Elastic IP is stable.
- Trying to enable hibernation on an existing instance. You must relaunch with hibernation configured.
- Calling
start-instancesright afterstop-instances. The instance may still bestopping. Useaws ec2 wait instance-stoppedfirst. - Terminating and expecting to recover. There is no undelete. Take a snapshot or create an AMI first if the data matters.
Exercise
You stop an instance named tutorial-web that has an auto-assigned public IP, one root EBS volume, and one instance store volume. You start it two hours later. List what is billed while it is stopped, and what you will find different after it starts.
Show answer
While stopped, you are billed for the EBS root volume (and any other EBS volumes), but not for instance usage. After the start, the instance store volume is empty because its data was lost, RAM contents and running processes are gone because the OS booted fresh, and the instance most likely has a new public IPv4 address. The private IPv4 address and the EBS data are unchanged.
Exercise
A teammate says, "I terminated the instance, so there are no more charges." Name two resources that could still be billing and the CLI command that deals with each.
Show answer
An Elastic IP address that is still allocated is one, and aws ec2 release-address --allocation-id <id> releases it. An EBS volume that was not deleted on termination is the other, for example an extra data volume, and aws ec2 delete-volume --volume-id <id> removes it. Snapshots also bill until deleted with aws ec2 delete-snapshot.