If you’re frequently stopping and restarting your EC2 instances, hibernation can save you money—It allows you to pause your instance like a virtual machine and restart it at another time, which can be used to drastically speed up loading times of On-Demand instances.
Hibernation Speeds Up Load Times
A common use case for On-Demand EC2 instances is quite literally spinning them up on demand, as necessary. If your demand is higher during mid-day, you might want to add an extra instance behind your load balancer to handle the extra traffic.
This is a great feature of AWS, but the downside is the startup time. Cold installs, such as when you create a new instance for the very first time, are the worst—AWS has to provision and set up your instance, then the AMI must install, then you must set up all your services. Rebooting the instance is faster, but still slow; the Linux server must start, then all your services must start. For applications with long loading times, this can be expensive, since you’re paying for this computation time often many times a day if you have a fleet of instances.
Hibernation allows you to save the entire contents of the machine’s RAM, and turn the machine off for an extended period of time. Then, when you need to start the instance again, the saved state is simply loaded back into RAM, exactly like how a virtual machine can be paused and restarted.
This can save money because the virtual machine can be restarted in seconds, rather than potentially minutes waiting for services to restart and come back online. Ultimately, if you’re constantly stopping and restarting your EC2 instances, you should turn on hibernation to speed up the process.
How To Use Hibernation
Hibernation is very simple to use, but it isn’t on by default, and must be enabled when creating the instance. You’ll see a flag under the Instance Configuration menu, for “Enable hibernation as an additional stop behaviour.” Turn this on.
One of the caveats of hibernation is that the storage disk must be an encrypted EBS volume. You can enable that on the next page by choosing the default encryption key from the drop down menu.
Once it’s running, you’ll see an option in the menu to hibernate the instance, under Actions > Instance State > Stop – Hibernate. You can also do this programmatically from the CLI with stop-instances and the –hibernate flag.
The contents of the RAM will be saved in memory and the instance will appear as “Stopped.” When you start it back up again, you should be able to reconnect in a few seconds.