A Quick Look At AWS EBS

Jonathan Duran
AWS in Plain English
6 min readAug 14, 2022

--

Elastic Block Storage (EBS) allows the persistence of data after an EC2 machine is terminated.

Let’s go over a few key points about storage as it pertains to EC2 instances.

  • Root device volumes contain the image used to boot the instance (this is the volume mounted to the / directory).
  • When launching an instance, the AMI shows if the root device volume is backed by Instance Store or EBS.
  • EBS is a network drive, meaning higher latency compared to Instance Store which is a physical drive attached to the instance.
  • Any data on the Instance Store volume is deleted once the instance is terminated.
  • By default, EBS has the DeleteOnTermination flag set to truefor the root volume.
  • An EBS volume can only be mounted to a single instance at a time and is bound to a specific AZ (you can get around this by creating a snapshot and restoring in a different AZ).
  • An EBS volume needs to be formatted with a filesystem and mounted to a directory after it has been attached to an instance.

Terraforming EC2 and EBS

First comes the network…

vpc.tf

This basic setup consists of a VPC with an Internet Gateway attached to it. We also have a subnet with a route table that routes all traffic to the internet gateway. Finally, to make SSH possible, we have a security group allowing all traffic to port 22. This security group will be associated with the EC2 instance created in the next step.

instance.tf

There are a few things happening within the instance.tf file. First, we are using a data resource to obtain the desired Ubuntu AMI for our instance. Next, the EC2 resource is configured. For the key_name , I used a previously created key which I named ec2Testing. Navigate to the AWS console, head to the EC2 dashboard, and click key pairs to create your own. Finally, we create an EBS volume with 8 GBs and attach it to our EC2 machine.

Create a new key pair.

Heads up! there are naming guidelines you should follow for the device_name. Depending on the block device driver of the instance kernel, the name of the volume may not be the same as the one you specify in the aws_volume_attachment Terraform resource.

Make sure to include a providers.tf file with the correct configuration for your AWS account. Finally, create the infrastructure with the commands terraform plan and terraform apply .

Checking the Console for the Resources

Now that we’ve Terraformed our infrastructure, let's check the console to confirm that everything was created as expected.

AWS Console, EC2

Notice the two EBS volumes attached to our instance. The volume named /dev/sda1 is our root volume and will be deleted once we terminate our instance. The other volume /dev/sdfis the one we created and will persist after the termination of our instance.

Mounting the Volume

To make use of our block device, we will SSH into our EC2 machine and format and mount our volume. Once data is written to the file system we chose to format, it is persisted in the EBS volume.

We can view all available disk devices along with their mount points using the command lsblk.

lsblk command shows available disk devices.

Note that the name we chose for the device, /dev/sdf was renamed to /dev/xvdf.

Use the sudo file -s /dev/xvdf command to check if the volume already has a file system. If you create a new filesystem on top of an existing one, the old data will be overwritten.

Checking for an existing filesystem.

This output means we have an empty device (Otherwise we’d get back information about the existing filesystem).

Let’s go ahead and create the filesystem using the mkfs command.

Creating a filesystem on the device.

And the directory we’ll use to mount to the volume.

Make the directory that will be mounted.

Now that we have a filesystem created on the volume and a new directory for the mount point, we can use the mount command to wrap things up.

Mount the directory and volume.

Note the /ebs the directory is now listed under the mount points for /dev/xvdf.

Automating the Mount

Say we reboot our instance, what then happens to the mount point of /dev/xvdf? Unfortunately, If we leave things as they currently are, we would need to manually re-mount the directory and volume every time the instance rebooted. Fortunately, we can automate this process by configuring the /etc/fstab file.

First, grab the UUID of the /dev/xvdf device using the lsblk -o +UUID command.

A new column was added to the lsblk command showing the device UUID.

Add the third line to the /etc/fstab configuration file. Make sure to use YOUR block device UUID.

🚨 You should backup this file!

The third line is what you should add to your config file (using your own UUID).

Verify there are no errors by running the command sudo umount /ebs to unmount the /ebs directory from the /dev/xvdf volume, then running sudo mount -a to remount. Assuming there are no errors, the mounting process should be done automatically following any instance reboots.

Attaching the Volume to Another Instance

Using the AWS console, create a second EC2 instance with the same configuration as the first (make sure to use the same security groups and key pairs).

Let’s first create a simple txt file in the /ebs directory. You can add any text into the ebs.txt file when opening vim, just make sure you save it.

Create an ebs.txt file and add anything to it.

Before we detach the EBS volume from the first instance, we need to unmount the /ebs directory from the device. Before running the sudo umount /ebs command, make sure to leave the /ebs directory as we cannot unmount a folder while we are in it.

In the console, search for the EBS volume and detach it from the instance.

Volumes in the EC2 dashboard.

Wait for the volume to become Available again and attach it to the new instance using the same Actions dropdown.

We now see the /dev/sdf volume attached to the new instance.

The new instance.

Now, SSH into the new machine, create a new directory (feel free to call it /ebs again) and follow the previous directions for mounting the device and directory. Keep in mind that you do not have to create a filesystem on the device.

Once the mount is complete, you should see the ebs.txt file with the same content you created on the first machine.

Devices in the new instance.

If you used Terraform for this exercise, please make sure to run terraform destroy to destroy all resources. You’ll have to manually delete the second EC2 instance as that was created outside of Terraform.

I hope this was a useful introduction to EBS. Stay tuned for more articles related to AWS, DevOps, and general programming. 👋🏼

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, and Discord.

--

--