30. EC2 Hibernate

EBS behavior: We can stop & terminate EC2 instances. If we stop the instance, the data is kept on the disk with EBS and will remain on the disk until the EC2 instance is started. If the instance is terminated, then by default root device volume will also be terminated.

Following things happen when we start EC2 instance:
i. OS boots up. Load windows or linux
ii. Run user data script (bootstrap scripts)
iii. Start applications (can take sometime) like mysql

EC2 Hibernate: When we hibernate an EC2 instance , the OS is told to perform hibernation (suspend-to-disk). Hibernation saves the contents from the instance memory (RAM) to EBS root volume. We persist the instances root volume and any attached EBS data volumes.

Starting an EC2 instance with EC2 hibernate:
When we start an instance out of hibernation:
i. The EBS root volume is restored to its previous state
ii. The RAM contents are reloaded
iii. The processes that were previously running on the instance are resumed.
iv. Previously attached data volumes are reattached & the instance retains its instance ID. So it keeps same instance ID as if we stop an instance or restart it.

With EC2 hibernate, the instance booths much faster. The OS doesnt need to reboot because the in-memory state (RAM) is preserved. This is useful for i. long running processed ii. services that take lot of time to initialize.

i. If we are going to use hibernation, the root device volume must be encrypted.
ii. EC2 hibernate preserves the in-memory RAM on persistent storage (EBS)
iii. Much faster to boot up because we do not need to reload the OS
iv. Instance RAM must be less than 150 GB
v. Instance families include C3, C4, C5, M3, M4, M5, R3, R4 & R5
vi. Available for windows, Amazon Linux 2 AMI & Ubuntu
vii. Instances cant be hibernated for more than 60 days
viii. Available for on-demand instances & reserved instances.

Question 1:
You have an in-memory database launched on an EC2 instance and you would like to be able to stop and start the EC2 instance without losing the in-memory state of your database. What do you recommend?
A• Create an AMI from the instance
B• Mount an in-memory EBS Volume
C• Use EC2 Instance Hibernate
D• Use an EC2 Instance Store
Answer: C
Explanation
Correct option:
Use EC2 Instance Hibernate
When you hibernate an instance, AWS signals the operating system to perform hibernation (suspend-to-disk). Hibernation saves the contents from the instance memory (RAM) to your Amazon EBS root volume. AWS then persists the instance’s Amazon EBS root volume and any attached Amazon EBS data volumes. When you start your instance: The Amazon EBS root volume is restored to its previous state The RAM contents are reloaded The processes that were previously running on the instance are resumed Previously attached data volumes are reattached and the instance retains its instance ID
For the given use-case, we must use EC2 Instance Hibernate, which preserves the in-memory state of our EC2 instance upon hibernating it.
Incorrect options:
Create an AMI from the instance – An Amazon Machine Image (AMI) provides the information required to launch an instance. You must specify an AMI when you launch an instance. You can launch multiple instances from a single AMI when you need multiple instances with the same configuration. You can use different AMIs to launch instances when you need instances with different configurations.
Creating an AMI won’t help, because it is a snapshot of an EBS volume, which represents all the files written on disk, not the state of the memory.
Use an EC2 Instance Store – An instance store provides temporary block-level storage for your instance. This storage is located on disks that are physically attached to the host computer. Instance store is ideal for the temporary storage of information that changes frequently, such as buffers, caches, scratch data, and other temporary content, or for data that is replicated across a fleet of instances, such as a load-balanced pool of web servers.
Using an EC2 Instance Store won’t help either, and we can’t stop an instance that has an instance store anyway.
Mount an in-memory EBS Volume – Amazon Elastic Block Store (EBS) is an easy to use, high-performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction-intensive workloads at any scale. In-memory EBS volumes don’t exist. This option has been added as a distractor.

Question 2:
A Machine Learning research group uses a proprietary computer vision application hosted on an EC2 instance. Every time the instance needs to be stopped and started again, the application takes about 3 minutes to start as some auxiliary software programs need to be executed so that the application can function. The research group would like to minimize the application boostrap time whenever the system needs to be stopped and then started at a later point in time.
As a solutions architect, which of the following solutions would you recommend for this use-case?
A• Use EC2 User-Data
B• Use EC2 Instance Hibernate
C• Use EC2 Meta-Data
D• Create an AMI and launch your EC2 instances from that
Answer: B
Explanation
Correct option:
Use EC2 Instance Hibernate
When you hibernate an instance, AWS signals the operating system to perform hibernation (suspend-to-disk). Hibernation saves the contents from the instance memory (RAM) to your Amazon EBS root volume. AWS then persists the instance’s Amazon EBS root volume and any attached Amazon EBS data volumes.
When you start your instance:
The Amazon EBS root volume is restored to its previous state
The RAM contents are reloaded
The processes that were previously running on the instance are resumed
Previously attached data volumes are reattached and the instance retains its instance ID
By using EC2 hibernate, we have the capability to resume it at any point of time, with the application already launched, thus helping us cut the 3 minutes start time.
Incorrect options:
Use EC2 User-Data – EC2 instance user data is the data that you specified in the form of a configuration script while launching your instance. Here, the problem is that the application takes 3 minutes to launch, no matter what. EC2 user data won’t help us because it’s just here to help us execute a list of commands, not speed them up.
Use EC2 Meta-Data – EC2 instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, for example, host name, events, and security groups. The EC2 meta-data is a distractor and can only help us determine some metadata attributes on our EC2 instances.
Create an AMI and launch your EC2 instances from that – An Amazon Machine Image (AMI) provides the information required to launch an instance. You must specify an AMI when you launch an instance. You can launch multiple instances from a single AMI when you need multiple instances with the same configuration. You can use different AMIs to launch instances when you need instances with different configurations.
Creating an AMI may help with all the system dependencies, but it won’t help us with speeding up the application start time.

Question 3:
A university manages a proprietary application on an EC2 instance. When started, the EC2 instance takes a long time to build a memory footprint for all the software libraries required for the application to function. The university would like to keep the instance pre-warmed so it can launch the analysis right away when needed.
Which of the following solutions would you recommend?
Options:
A• Use a custom AMI with the software libraries pre-installed
B• Use Spot Instances
C• Create an Auto Scaling Group (ASG) with capacity 0
D• Use EC2 hibernate
Answer: D
Explanation
Correct options:
Use EC2 hibernate
When you hibernate an instance, AWS signals the operating system to perform hibernation (suspend-to-disk). Hibernation saves the contents from the instance memory (RAM) to your Amazon EBS root volume. AWS then persists the instance’s Amazon EBS root volume and any attached Amazon EBS data volumes.
When you start your instance:
The Amazon EBS root volume is restored to its previous state
The RAM contents are reloaded
The processes that were previously running on the instance are resumed
Previously attached data volumes are reattached and the instance retains its instance ID
Sicnce EC2 hibernate allows RAM contents to be reloaded and previously running processes to be resumed, hence this option is correct.
Overview of EC2 hibernation via – https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Hibernate.html
Incorrect options:
Use a custom AMI with the software libraries pre-installed – Custom AMI will not help as the required software libraries would still need to be loaded in the RAM, causing a delay in processing. Custom AMIs cannot be used to pre-warm the instances.
Use Spot Instances – A Spot Instance is an unused EC2 instance that is available for less than the On-Demand price. Because Spot Instances enable you to request unused EC2 instances at steep discounts, you can lower your Amazon EC2 costs significantly. You cannot use spot instances types to pre-warm the instances.
Create an Auto Scaling Group (ASG) with capacity 0 – Using Auto Scaling Group (ASG) with capacity 0 implies that you will not have any running instances when there is no traffic. You cannot use this to pre-warm the instances.