In the world of Amazon Web Services (AWS), data reliability and recovery are paramount. That's where snapshots come into play. AWS provides two primary types of snapshots: EC2 snapshots and EBS (Elastic Block Store) volume snapshots. In this blog, we will explore both, along with practical examples.
EC2 Snapshots
EC2 (Elastic Compute Cloud) instances are the virtual machines that run in AWS data centers. EC2 snapshots are a way to back up the data on your EC2 instances, including the root volume and additional data volumes attached to them.
Firstly, we are launching an instance that has a website. Say, Apache Web is installed in it. The website must display my name when launched. We are taking reference of the previous blog https://kshitijaa.hashnode.dev/setting-up-an-application-load-balancer-with-aws-ec2
Creating an EC2 Instance
I will be launching AWS instance with the following install.sh file to run after launching this instance. You can consider this as UserData also. But, the purpose of using install.sh is to have this file in our root volume to take a snapshot of this same instance and its root volume!
#!/bin/bash
sudo apt update -y
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2
cd /
cd /var/www/html
sudo rm index.html
sudo touch index.html
sudo chmod 777 index.html
sudo cat > index.html << EOF
<header>
<h1>Hello World! Welcome to <a href="https://kshitijaa.hashnode.dev/" target="_blank">Kshitija-Bartakke-Malwade-Blogs!</h1>
</header>
EOF
sudo systemctl restart apache2
sudo systemctl status apache2
Once your instance is launched, run install.sh and browse the public Ip address of this instance. You will see the following output.
Creating an EC2 Snapshot
Let's say you have an EC2 instance with the ID i-0571665bde4645438, and you want to create a snapshot of its root volume.
Open the AWS Management Console and navigate to the EC2 dashboard.
Select "Snapshot" on the left sidebar.
Click on "Create Snapshot"
Select following: Resource Type : Instance , Instance ID : i-571665bde4645438, Description: Apache-Web-Instance-Configured
Keep all other things as default and click "Create Snapshot"
Now, you have a point-in-time backup of your EC2 instance's root volume.
EBS Volume Snapshots
EBS volumes are block storage devices that can be attached to EC2 instances. EBS volume snapshots are a way to back up these volumes independently, without needing to create an EC2 snapshot.
Creating an EBS Volume Snapshot
Let's assume you have an EBS volume with the ID vol-0123456789abcdef0
, and you want to create a snapshot of it.
Open the AWS Management Console and navigate to the EC2 dashboard.
Select "Snapshots" on the left sidebar.
Click "Create Snapshot."
Select the following: Resource Type: Volume, Volume ID: Select Root Volume of your instance, Description: Root-Volume-Apache2-Web
Optionally, you can add tags to the snapshot for better organization.
Click "Create Snapshot."
Now, you've created a snapshot of your EBS volume, which is stored independently and can be used to create new volumes or restore data in case of issues.
Create EC2 Instance using your Snapshot
Restoring an EC2 instance from a snapshot primarily involves creating a new EBS volume from the snapshot and then attaching it to a new EC2 instance. This process can be a lifesaver if your original instance becomes irreparable, or you want to duplicate an existing environment.
We need to create an Image of this snapshot, to create an instance
Creating an image (often called an AMI or Amazon Machine Image) directly from an EBS snapshot isn't as straightforward as one might expect. The snapshot represents the state of an EBS volume, while an AMI represents an entire EC2 instance โ including all attached EBS volumes and instance metadata.
However, you can create an AMI by manually creating an EC2 instance using a volume restored from a snapshot and then creating an image from that instance. Here's how:
Go to the Snapshots window, select the EC2 Instance snapshot as we have done above. Apache-Web-Instance-Configured
Go to Actions and Create image from snapshot
Give Image name its description, keep all other settings default.
Image created successfully!
Select this Image and Launch instance from AMI
Mention Name of the instance, should select SG and KeyPair! I had missed it and modified the later.
Launched instance should get all the Apache server configuration.
Connect to the instance and here we see the configuration is well received!
Browse the public IP address of this instnace.
BOOM!!! You see the same website that we have configured before!
We have successfully launched EC2 Instance and Website from Instance Snapshot!
Why Are Snapshots Important?
Data Recovery: Snapshots act as a safety net. In the event of data loss or corruption, you can restore your system or data volumes to a previous state.
Data Cloning: You can create new volumes from snapshots. This is useful for cloning environments or moving data across regions.
Backup and Disaster Recovery: Snapshots are a key component of your backup and disaster recovery strategy.
You can also put the snapshot files to s3 and use them later!
Cost Considerations
Keep in mind that while creating snapshots is an essential part of AWS data management, it's not entirely free. You are charged for the storage space used by your snapshots. Additionally, there might be a cost associated with copying snapshots across regions or accounts.
Conclusion
Snapshots in AWS, whether EC2 snapshots or EBS volume snapshots, are a critical component of your data management and disaster recovery strategy. They provide you with the flexibility to protect and restore your data, ensuring business continuity and peace of mind in the dynamic world of cloud computing.