DEV Community

Cover image for Connecting EC2 Instances to AWS Block Storage Made Easy
Al-Amin Firdows
Al-Amin Firdows

Posted on

Connecting EC2 Instances to AWS Block Storage Made Easy

In the world of cloud computing, Amazon Web Services (AWS) provides a robust ecosystem of services to power your applications and infrastructure. Amazon Elastic Compute Cloud (EC2) offers virtual servers in the cloud, while AWS Block Storage, specifically Amazon Elastic Block Store (EBS), provides scalable and reliable storage solutions. In this blog post, we'll guide you through the process of connecting EC2 Linux instances to AWS Block Storage, simplifying the setup for both operating systems.

Prerequisites

Before we dive into the steps, ensure you have the following prerequisites in place:

  1. An AWS account: Sign up for an AWS account if you haven't already and log in to the AWS Management Console.
  2. EC2 Linux instance: Launch an EC2 Linux instance based on your requirements. You can choose the appropriate instance type, storage, and configuration that aligns with your workload.
  3. AWS Block Storage (EBS): Provision an EBS volume with the desired size and performance characteristics, ensuring it is in the same AWS region as your EC2 instance.

.

Step 1: Identify the EBS Volume

  1. Go to the AWS Management Console and navigate to the EC2 Dashboard.
  2. Select the Linux-based EC2 instance you want to connect with the EBS volume.
  3. From the "Actions" dropdown menu, choose "Attach Volume."
  4. In the "Attach Volume" dialog box, select the desired EBS volume from the list and click "Attach."

.

Step 2: Identify the Attached Volume

Once you are connected to the EC2 instance, run the command lsblk to list the available block devices.

lsblk
Enter fullscreen mode Exit fullscreen mode

Look for a device with a name similar to "/dev/xvdf" or "/dev/nvme1n1," indicating the attached EBS volume.

.

Step 3: Check for Existing File System

Run the command sudo file -s /dev/xvdf (replace "/dev/xvdf" with the appropriate device name) to check if there is a file system on the volume.

sudo file -s /dev/xvdf
Enter fullscreen mode Exit fullscreen mode
  • If the output shows "data" or "no file system found," it indicates that there is no file system on the volume, and you need to create one.
  • If the output shows information about the file system type (e.g., "SGI XFS filesystem data," "ext4 filesystem," etc.), it means that a file system already exists on the volume.

.

Step 4: Create a File System (If not already created)

Run the command sudo mkfs -t xfs /dev/xvdf (replace "/dev/xvdf" with the appropriate device name) to create a new XFS file system on the EBS volume. Confirm the operation by typing "y" when prompted.

sudo mkfs -t xfs /dev/xvdf
Enter fullscreen mode Exit fullscreen mode

or sudo mkfs -t ext4 /dev/xvdf to create a new ext4 file system.

.

Step 5: Mount the EBS Volume

Create a directory where you want to mount the EBS volume using the command sudo mkdir /mnt/myvolume (replace "/mnt/myvolume" with the desired directory path).

sudo mkdir /mnt/myvolume
Enter fullscreen mode Exit fullscreen mode

Mount the EBS volume to the specified directory using the command sudo mount /dev/xvdf /mnt/myvolume (replace "/dev/xvdf" with the appropriate device name and "/mnt/myvolume" with the mount point).

sudo mount /dev/xvdf /mnt/myvolume
Enter fullscreen mode Exit fullscreen mode

Change to the /mnt/myvolume directory and check the disk space to validate the volume mount.

cd /mnt/myvolume
df -h .
Enter fullscreen mode Exit fullscreen mode

The above command should show the free space in the /mnt/myvolume directory.

.

Step 6: Enable Automatic Mounting (Optional)

To automatically mount the EBS volume on instance reboot, you have to add a command to the /etc/fstab file. Before making changes, back up the /etc/fstab file for safekeeping.

sudo cp /etc/fstab /etc/fstab.bak
Enter fullscreen mode Exit fullscreen mode

Open the /etc/fstab file with the command sudo nano /etc/fstab.

sudo nano /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Add the following line at the end of the file:

/dev/xvdf /mnt/myvolume ext4 defaults,nofail 0 0
Enter fullscreen mode Exit fullscreen mode

(replace "/dev/xvdf" and "/mnt/myvolume" with the appropriate device name and mount point).

.

Congratulations! You've successfully connected your EC2 Linux instance to AWS Block Storage (EBS) by following these straightforward steps. With this setup, you can now leverage the scalability, durability, and performance of AWS Block Storage for your applications and data storage needs. Remember, as your requirements evolve, AWS provides a range of options to optimize and manage your block storage, including features like snapshots, encryption, and volume resizing. Explore the AWS documentation to unleash the full potential of your EC2 instances and EBS volumes.

Harness the power of AWS and unlock new possibilities with seamless connectivity between your EC2 Linux instances and AWS Block Storage. Happy cloud computing!

Note: It's essential to refer to the official AWS documentation for the latest updates and detailed instructions related to EC2 instances and EBS volumes.

Top comments (0)