Regularly stopping and restarting your EC2 instances can save you money from hibernation: you can pause your instance like a virtual machine and restart it at another time, which can be used to dramatically speed up On-Demand load times . cases.
Sleep mode speeds up loading times
A common case for On-Demand EC2 instances is that they are literally activated on demand when needed. If your demand is higher mid-day, you may 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 boot time. Cold installations, like when you create a new instance for the very first time, are the worst: AWS has to provision and set up your instance, then install the AMI, and then you have to set up all your services. Instance restart is faster, but still slow; the Linux server should start, then all your services should start. This can be expensive for applications with long loading times, as you often pay for this compute time several times a day if you have a large number of instances.
Hibernation allows you to save the entire contents of the machine̵
This can save money because the virtual machine can be restarted in seconds, rather than possibly waiting minutes for services to restart and come back online. If you are constantly stopping and restarting your EC2 instances, you should enable hibernation to speed up the process.
The feature is available at no additional cost, except, of course, for the contents of the RAM that is stored on the EBS volume during hibernation. It is available on C5, I3, M5, R5 and T3 series instances (as well as older versions in each class) with Amazon Linux, Amazon Linux 2, Ubuntu 16.04 and 18.04 LTS, as well as Windows Server 2012, 2016 and 2019 Windows servers can only hibernate with 16 GB RAM, but Linux servers support up to 150 GB RAM.
Hibernation is very easy to use, but it is not on by default and must be enabled when the instance is created. You will see a flag under the Instance Configuration menu for ‘Enable hibernation as an additional stop behavior’. Turn this on.
One caveat about 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 is running, you will see an option in the menu to put the instance to sleep, under Actions> Instance State> Stop – Hibernate. You can also do this programmatically from the CLI with
stop-instances and the flag hibernation.
The contents of RAM are stored in memory and the instance is displayed as “Stopped”. When you reboot it, you should be able to reconnect in a few seconds.