Hibernation mode in EC2 Instance
When in sleep mode the RAM preserves the current state of all the processes however it will persist as long as the power gets supplied to machine. If the battery is drained the data stored in RAM is lost permanently.
In case of Hibernation the state of current process is stored in hard disk (persistent storage). This will help us in long running batch jobs which are running for several days. We can put the system in hibernation mode so that the state of current processes is stored in hard disk & the data is never lost due to this.
In order to achieve this we need to enable below option while launching EC2 instance. This option can’t be set once the instance gets launched.
Hibernation is supported only in Amazon Linux (Linux II) & Ubuntu instances. Red Hat/CentOS does not support this.
We want to manage this instance using AWS System Manager (AWS-SSM).
So , we’ll create a IAM role & attach to this instance while launching.
Also, we need to enable root volume encryption using Amazon provided Key (AWS-KMS)
Pls note that for free-tier max. limit of EBS volume if 30 GB (Gp2-SSD)
Since we are going to connect to this instance using AWS-SSM , we’ll not attach any security group to this instance.
The advantages of not using any security group to this EC2 instance is as follows
- We don’t have to open any port (e.g. 22 for SSH)
- No need to use Bastion host
- More secure connection through AWS-SSM as we are using IAM role attached to EC2 instance
- It can be integrated with CloudWatch for monitoring through SSM
Let’s use tags so as to form resource group in AWS-SSM
Once the EC2 Instance is up & running , we can create resource group in SSM as under
Note that all the instances with Environment tag value as Dev will fall into the resource group below
In order to connect to this instance we’ll use SSM-Session Manager as below
After clicking on Start Session following terminal window will be displayed.
Now , let’s start one process called sleep as below
Now we’ll change the state of instance to hibernate as below
This will stop the instance as can be seen below.
Pls note that the current state of all processed will be stored in root volume after this.
We can confirm that the instance is stopped using AWS console
Now, let’s start the instance through console & connect to it sing AWS-SSM
We can see that there is a change in uptime as under.
Also, we can see the sleep process which we had started before hibernating the EC2 instance. It has not stopped & the state of the process has been stored in RAM before we changed the state of EC2 instance to Hibernate.
Industry Use Cases
Thus using hibernate we can save cost as our EC2 instance will not be running unless we start it. This is a great step towards cost optimization. This concept is very useful when we have very long running jobs which are running for a long time e.g. batch jobs/ETL jobs running for almost 6–7 days. In this cases we can hibernate the instances at the end of the day & resume it the next day.
The only caveat is that the instance will take sometime to start as it has to restore the state of all the processes stored in RAM. However, on the cost front we’ll be on the positive note as AWS will not charge when EC2 instances are not running…
Hope this explains the Hibernate concept quite well..
Thanks for reading the article !