Guided Lab: Managing Instance Volumes Using Elastic Block Store (EBS)

Description

In AWS, you can effortlessly attach and detach EBS volumes from one EC2 instance to another. This flexibility is great for things such as upgrading instances or moving data around without much hassle. It’s a handy feature for scaling your storage needs or handling system maintenance with minimal disruption.

Throughout this hands-on lab, you will gain experience in creating an EC2 instance with an additional EBS volume. You will learn to attach and detach an EBS to/from a specific EC2 instance and learn the process of volume deletion.

Prerequisites

To ensure successful completion of this lab, you must have prior experience in creating EC2 instances and be familiar with their essential components. If you feel that your knowledge in this area is insufficient, we highly recommend taking the following labs to gain the necessary understanding:

  • Creating an Amazon EC2 instance (Linux)
  • Creating an Amazon Machine Image (AMI) from an EBS-backed EC2 instance
  • Familiarity with basic Linux commands is beneficial but not required
  • Connecting to your EC2 instance using EC2 Instance Connect

Objectives

In this lab, you will:

  • Create an EC2 instance with an additional EBS volume.
  • Create a new EBS volume.
  • Attach and Detach an EBS volume

Lab Steps

Creating an EC2 instance

Create two EC2 instances using the following configurations:

  1. Name: agila1 & agila2
  2. Instance type: t2.micro
  3. AMI: Linux
  4. Key pair: Create a new Key Pair
    • Key Pair name: agila_keys
    • Key Pair Type: RSA
    • Private key file format: .pem
    • Click Create key pair

Review your instance configurations and click the “Launch Instance” button.

Creating a new EBS volume

1. On the Volumes listing page, make sure to take note of the Availability Zone:
Note: Create the volume in the same availability zone. If you do not do this, you will not be able to attach the volume.

2. Create a new volume, click on Create volume. This will take you to a Volume setting page and set the following values before clicking Create volume. 

  • Volume Type: General purpose SSD (GP2)
  • Size10 GiB
  • Availability Zone: us-east-1a ( Depends on where the AZ of your EBS Volumes are)

3. Wait until the Volume state is Available (refresh the page every 10/15 seconds):

Creating a change on the EBS Volumes and reattaching it to an EC2 instance

To attach the newly created EBS Volume, follow the steps below:

  1. Right-click on the newly created EBS volume and select Attach Volume.

2. Select the instance ID of the agila1 instance. Then, click on the Attach volume button.

Note: The device name may be automatically renamed by newer Linux kernels, even when it is initially entered as /dev/sdf.

Format and mount an attached volume

1. Connect to the agila1 instance using EC2 Instance Connect.

2. Check the available disk devices and their mount points using this command.

lsblk

The /dev/xvda device is the root EBS volume of the instance, which has three partitions named xvda1, xvda127, and xvda128. The EBS volume on /dev/xvdf is the new 10GB volume that we created. We have to format it first and then mount it for it to be usable.

3. Now, format the Data Volume with an ext4 file system by using this script.

sudo mkfs -t ext4 /dev/xvdf

4. Next, create a mount point in the Data Volume. Use the mkdir command. The mount point is where the volume is located in the file system tree and where you read and write files after you mount the volume. In this lab, create a directory named /playcloud.

sudo mkdir /playcloud

5. Mount the volume or partition to the /playcloud mount point.

sudo mount /dev/xvdf /playcloud

6. Verify if the Data Volume is successfully mounted. Use lsblk -f command to view your available disk devices and their mount points.

lsblk -f

After mounting the Data volume, we will make changes to it and reattach it to the agila2 instance. Finally, we will check if the changes persist after reattachment.

7. Now, go to the /playcloud directory and create a file inside.

#Switch your user account
sudo su

#Create a file txt
echo "Welcome Tutorials Dojo! Happy Learning!" > message.txt

#Check the if the file is created
ls

8. Go back to the Volumes listing page. Right-click on the Data Volume → select the Detach volume action and click Detach in the confirmation dialogue box.

9. Now, attach the Data Volume to the agila2 instance.

10. Next, connect to your agila2 instance using EC2 Instance Connect. Check the available disk device and their mount points.

11. Since we have already formatted the Data Volume, we only need to create a directory and mount it again to the agila2 instance.

#Add a directory
sudo mkdir /playcloud

#Mounting the EBS Volume
sudo mount /dev/xvdf /playcloud

12. Verify if the file added earlier exists.

cd /playcloud
ls

That’s it! Congratulations on completing the basics of managing your Instance Volumes using Elastic Block Store (EBS)! As you continue your journey, remember that EBS offers additional features and optimizations to enhance your storage experience. Happy learning!

Skip to content