Pre-requisite

To understand this blog, you should have basic knowledge of AWS EC2.

You can study EC2 from here.

Content of this blog

  1. Introduction to EBS
  2. Types of EBS Volume
  3. Creating an EBS Volume and attaching it to an EC2 instance
  4. Changing the size of existing EBS volume without downtime
  5. Snapshots and Backups
  6. Cleanup all resources

We will do everything with the hands-on tutorial on AWS Console.

Introduction to AWS EBS (Elastic Block Store)

When using a Windows laptop, you have storage (such as SSD or HDD) on C-Drive, D-drive, etc.

Similarly, the EC2 instance has also some storage. Remember, when we were creating our EC2 instance in the previous blog then we gave it 8 GB storage of type gp3 (actually we didn’t give it, it was given by default and we haven’t changed it).

See the below image. It is taken from step 5 of deployment from my previous blog.

It was an EBS volume of 8 GB.

There are generally two kinds of storage types in EC2 servers:

  • Instance Store
  • EBS Volumes

Instance Store

This is the storage type that is physically attached to the host machine (EC2) running. Data on an instance store is lost when the instance stops or terminates.

Use Case: It is ideal for temporary storage of information that changes frequently, such as caches, buffers, or scratch data.

Since it is physically attached, it gives very high performance because data packets don't have to travel from somewhere else to your ec2 instance.

EBS Volumes

Think of it as an external hard drive that is plugged into your EC2 instance. This means you can attach or detach it to your instance anytime. Even if your instance gets terminated it won’t affect your EBS volume and you can attach this EBS volume to some other EC2 instance.

Use Case: It is suitable for databases, file systems, or any application that requires persistent storage.

Let me clarify one thingEBS volume is not created inside the EC2 instance. It sits outside within the same availability zone.

An Instance Store is created inside the EC2 machine means it is the part of the machine but the EBS volume is kept away from the EC2 machine (maybe 40–50 meters away we don’t know) but the EBS volume is in a different machine from our instance. When we connect the EBS volume to the EC2 instance, it doesn’t connect it physically but virtually and the data travels from EBS to EC2 via some network. Relate it with how data transfers via Bluetooth between two mobiles.

Since the Instance Store is inside the EC2 machine, it has low latency as compared to EBS but its data gets lost when the instance gets terminated. EBS is more reliable as its data persist even after our instance gets terminated.

Important Note: An EBS volume can only be attached to an EC2 instance that resides in the same Availability Zone where the EBS volume was created. For example, if an EBS volume is created in ap-south-1b, it can only be attached to an EC2 instance in ap-south-1b.

Types of EBS Volumes

AWS provides different kinds of EBS volume with different pricing and we can choose anyone based on the type of application we are building.

The below data is taken from AWS documentation. No need to remember these.

  1. General purpose SSD (gp2 and gp3): Suitable for any general task. gp2 offers up to 16,000 IOPS (input/output operations per second) and 1,000 MB/s throughput while gp3 provides up to 16,000 IOPS with performance scaling linearly with volume size (3 IOPS per GB, up to 16,000 IOPS at 5,334 GB).
  2. Provisioned IOPS SSD (io1 and io2): High-performance SSD volumes designed for critical applications that require consistent and low-latency IOPS. Ideal for large databases like SQL Server, Oracle, and MySQL that require high IOPS and consistent performance. io2 offers higher durability (99.999% durability) and performance compared to io1, with up to 64,000 IOPS and 1,000 MB/s throughput while io1 provides up to 64,000 IOPS and 1,000 MB/s throughput, with the ability to specify an IOPS rate when creating the volume.
  3. Throughput Optmizatized HDD (st1)
  4. Cold HDD (st2)

I am not writing the numerical details of throughput, IOPS etc for the 3rd and 4th types. You can check out every detail about it from here.

Enough theory, now we will do practical of everything that we studied. Be ready with your AWS Console.

Creating an EBS Volume and attaching it to an EC2 instance

Visualization Tutorial

  1. We will launch our EC2 Instance (called EC2 Instance 1) with a default EBS Volume of 9 GB

2. We will attach another external 4 GB EBS Volume and attach to this EC2 Instance 1.

3. Download a code from GitHub to this external EBS Volume of 4GB.

4. Delete EC2 Instance 1. If we delete it then our default 9 GB EBS Volume will also get deleted. If we don’t want to delete this default 9 GB volume then we have to just select a “not delete” dropdown while launching this instance. But we will not do that

5. Again launch another EC2 Instance 2 (with a default root volume of 10 GB) and attach the same previous EBS volume to the new EC2 instance then check whether the code that we have downloaded before persists or not.

By doing this exercise, we will understand that we can create EBS Volumes independently and attach them to any EC2 instance. And even if our EC2 instance gets terminated, it won’t affect our EBS Volumes.

Actual Tutorial

  1. Launch an EC2 instance. I have shown this in the previous blog. So, will not show here, how to launch an instance.
    While giving EBS volume during the launch, I changed the default value from 8GB to 9 GB.

In the below image, you can see that our instance is launched in “ap-south-1a” availability zone. Please note, as I told you before, we have to create EBS volume in the same availability zone.
I named my instance as “shivam-ebs-tutorial”.

You can see on the left sidebar, there is “Elastic Block Store”. Click on “Volumes” to go to the EBS dashboard.

You can see that there is a 9 GB volume created. It was created while we were launching our instance above.

When we will terminate our instance then this 9 GB volume will get deleted because we haven’t configured it to not delete while launching the instance. If you don’t want your default EBS volume to be deleted with your instance, see the image in step 1, Click on “advanced” and uncheck the select box of delete.

Back to the topic, Click on “Create Volume” as you can see in the upper right corner.

2. I selected the Volume type as gp2 and gave 4 GB size. And the same region “ap-south-1a” as our instance. Then click on “Create Volume” on the bottom.

3. Just gave the name “my-ebs-volume” to our newly created EBS Volume for simplicity and understanding.

4. Select “my-ebs-volume” checkbox then Click on “Actions” then click on “Attach volume” to attach it to our ec2 instance.

5. Select the instance (in the same availability zone) for which you want to attach this Volume. I have selected the same instance “shivam-ebs-tutorial” as I launched above.
You can select any device type, it won’t matter. Then click on “Attach Volume” button.

6. Now, volume “my-ebs-volume” is attached to the instance “shivam-ebs-tutorial”. We have reached till image 2 of our visualization tutorial.
SSH to my EC2 machine to download code on “my-ebs-volume” to reach image 3.

7. Everything we will do on this EC2 instance, we have to do in the terminal because there is no GUI available here.
We have to check, what block devices (hard disk, EBS Volumes) etc attached to this machine. The below command is for this:

lsblk

You can see, there are two volumes: one is “xvda” of 9 GB (this is the default root volume that we gave during the launch instance) and the second is “xvdf” of 4 GB (this is “my-ebs-volume” we created. Here we will download our code).

If it is your first time and you don’t know all the below steps that I am doing with Linux commands then just cram it here. If you know Linux then these are very common for you.

  • First, we have to enable the File System for newly created xvdf storage. If you are attaching any existing EBS volume then you don’t need to do this. But, since we created this EBS Volume fresh and haven’t enabled the file system till now, we have to do it.
    To check file system is enabled or not, type the below command
sudo file -s /dev/xvdf

If the response comes as “/dev/xvdf: data”, then it is not enabled.

  • Enable File System, type the below command
sudo mkfs -t xfs /dev/xvdf
  • Make a folder and mount that file system into this folder. Now all the things that you download or make in this folder will be created in the file system of EBS Volume.

Make a folder with the name “myFileStorage” from the below command:

mkdir myFileStorage

Mount the file system of “xvdf” into this folder from below command:

sudo mount /dev/xvdf myFileStorage
  • Now, our EBS volume is mounted to folder “myFileStorage”. Whatever we make inside this folder will be kept inside our EBS Volume.
    Download this code into this folder by doing git clone.
  • Finally, we completed till Image 3 of our visualization tutorial.

8. Terminate the instance “shivam-ebs-tutorial”.

Completed till image 4 of the visualization tutorial.

9. Launch a new EC2 Instance with name “2-shivam-ec2-tutorial” with default root EBS Volume 10 GB.

While launching EC2 Instance, go to the network setting and select a subnet of “ap-south-1a”. It will launch our instance in ap-south-1a. We are doing it because our EBS Volume is in ap-south-1a. Don’t worry, if you don’t know about the subnet. We will study it in detail in upcoming blogs just like we are studying EBS Volume now.

Click on “Launch Instance” and our instance named “2-shivam-ebs-tutorial” launched in ap-south-1a.

10. Now, go to Volumes and select the same EBS Volume “my-ebs-volume” to attach it to our new EC2 instance.

Select the newly created EC2 Instance and any device type.

11. Do SSH in the new EC2 instance “2-shivam-ebs-tutorial” and run lsblk to check if the volume is correctly attached or not.

Since we already created the file system of this EBS Volume so we don’t need to do it now. Just make any folder in this EC2 instance and attach this file system to that folder. Then check if the code that we installed previously persists or not.

df -k command is to check the mount folder.

You can see in the above image that when we do “ls”, we get the same data that we downloaded previously. This shows data persists.

And finally, we complete image 5 of the Visualization Tutorial.

This was a little boring but if you read carefully till now, then I hope you understood everything.

Changing the size of existing EBS volume without downtime

  1. Select the EBS Volume by clicking the checkbox
  2. Click to “Action” button on the top right corner
  3. Click on resize volume then give the new volume size.
    Note: You can only increase the size, which means if you have initially given 4 GB then you can only give a size which is greater than 4GB.
  4. Now, SSH to your EC2 instance and type the below command
resize2fs /dev/xvdf

5. Your volume has been resized. You can check your new size that has been increased from the below command:

df -h

Snapshots and Backups

Let’s start with an example, I’m assuming, you know the basics of git. In git whenever we do some changes we “commit” it. Commits help us to track logs. Suppose one feature that we are building is not good then we can roll back to the previous commit. This means it helps us to maintain the history.

In the same way, when we do some changes in our EBS Volume, download or delete some data etc, we can create a snapshot of it.

Now, the snapshot is a separate entity. With the help of a snapshot, we can create an EBS Volume. And it will have all the data up to that point when we capture the snapshot. Suppose, in future, our volumes get deleted still our data doesn’t get lost because we have taken a snapshot of it and we can create another EBS Volume which has all the data with the help of the snapshot. So, snapshots are also used to maintain the backup of the data.

In some companies, they take snapshots of their EBS Volume every day at night. So, they have the data backup for each day.

There is another very cool advantage of snapshots. They are region-specific. This means a snapshot created from ap-south-1a, ap-south-1b, all will be created in the region ap-south-1. And you can create EBS Volume with the help of the snapshot in any availability zone of the snapshot region.

Suppose you want to attach an EBS Volume of ap-south-1a to an EC2 instance of ap-south-1b. What you can do is, create the snapshot of ap-south-1a EBS volume, then launch another EBS Volume in the zone ap-south-1b with the help of the snapshot then attach it to the instance which is running on ap-south-1b. This is a good exercise. Do, it as homework.

Tutorial on how to create a Snapshot

We will create a snapshot from our same EBS Volume “my-ebs-volume”. Since, that volume was present on ap-south-1b, so our snapshot will be created at the ap-south-1 region.

  1. Select the checkbox of the EBS Volume, then click on “Action” then click on “Create Snapshot”.

2. You can give any description but generally people give the date as the description to have the record of when this snapshot was created. Then click on “Create Snapshot” button.

3. Go to Snapshots just below Volumes in the left sidebar.
Our snapshot is created in the region ap-south-1.

4. As homework, your task is to create EBS Volume from this snapshot in the region ap-south-1b then attach it to an instance. It’s very easy.

Cleanup all resources

We will delete/ terminate all the resources which we have created in this tutorial so you will not get any bill from AWS.

  1. Terminate the EC2 Instances that we created.
    You can see this in step 8 of the tutorial.
  2. Delete EBS Volume.

3. Delete Snapshot

Post a Comment

Previous Post Next Post