How to Attach an EBS Volume to Multiple EC2 Instances

How to Attach an EBS Volume to Multiple EC2 Instances

Step-by-Step Guide: Attaching an EBS Volume to Multiple AWS Instances

Β·

5 min read

Hello friends, my name is Bilal Khan. Today, I will guide you through the process of attaching an Elastic Block Store (EBS) volume to multiple Amazon EC2 instances. This tutorial is particularly useful for those who are new to Amazon Web Services (AWS) and want to understand how to utilize EBS volumes more effectively.

In this step-by-step guide, we will create EC2 instances, set up an EBS volume, and demonstrate how you can attach that EBS volume to multiple instances. By the end of this tutorial, you will have a clear understanding of how to manage EBS volumes in a multi-instance environment.

Here is the video explanation of it:

What is an EBS Volume? πŸŽ‰

Before diving into the process, let’s take a moment to understand what an EBS volume is. Amazon Elastic Block Store (EBS) provides block-level storage volumes for use with EC2 instances. These volumes are used for storing data, and they persist independently from the life of an instance. EBS volumes can be attached to a single instance or multiple instances, depending on the configuration.

Creating EC2 Instances πŸ–₯️

Let's start by creating the EC2 instances.

  1. Go to the Instances Page: Open your AWS Management Console, navigate to the EC2 service, and select "Instances" from the sidebar.

  2. Create a New Instance: Click on the "Launch Instance" button. This will open a new page where you can configure the details of your instance.

  3. Select an Amazon Machine Image (AMI): Choose an Amazon Machine Image (AMI) for your instance. For this tutorial, we'll select Ubuntu as the AMI.

  4. Choose an Instance Type: Next, select the instance type. We'll use the t2.micro instance type, which is suitable for testing purposes.

  5. Configure Instance Details:

    • Select your desired network (VPC).

    • Assign a public IP automatically.

    • Choose the subnet that best fits your network needs.

  6. Add Storage: By default, an EBS volume will be attached to your instance as the root volume. We’ll discuss adding additional volumes shortly.

  7. Launch the Instance: After configuring these settings, click "Launch" to start the instance creation process.

Renaming and Initializing Instances ✍️

Once the instances are launched, they will appear in your instances list.

  1. Rename the Instances: To keep track of them, rename the instances to something more descriptive like EBS-Volume-1 and EBS-Volume-2.

  2. Check Initialization: It may take a few minutes for the instances to initialize. You can monitor their status on the Instances page.

Creating an EBS Volume πŸ“‚

Now, let’s create an EBS volume that we can attach to these instances.

  1. Go to the Volumes Page: In the EC2 dashboard, click on "Volumes" in the sidebar to see your existing EBS volumes.

  2. Create a New Volume:

    • Click on "Create Volume."

    • Choose the volume type. In this case, we'll select io2, which supports multi-attach functionality.

    • Specify the size of the volume (e.g., 5 GB).

    • Enable the "Multi-Attach" option. This is crucial for attaching the volume to multiple instances.

  3. Create the Volume: Once you've configured the settings, click "Create Volume." The new volume will appear in your volumes list.

Attaching the EBS Volume to Multiple Instances πŸ”—

With the EBS volume created, we can now attach it to our instances.

  1. Select the Volume: Find the volume you just created in the volumes list and click on it.

  2. Attach Volume:

    • Click on the "Actions" button and select "Attach Volume."

    • In the attachment page, choose the first instance (EBS-Volume-1) from the dropdown menu and click "Attach."

    • Repeat the process for the second instance (EBS-Volume-2).

  3. Verify Attachment: Once the volume is attached to both instances, it should show as "in-use" with multiple attachments in the volumes list.

Accessing the EBS Volume on Your Instances πŸŽ‰

Now that the volume is attached, let’s access it from within the instances.

  1. SSH into Your Instances: Use SSH to log into each of the EC2 instances.

  2. List Attached Volumes:

    • Run lsblk command to list the block devices attached to your instance.

    • You should see the new EBS volume listed.

  3. Mount the Volume:

    • Create a directory where you want to mount the volume, e.g., sudo mkdir /mnt/ebs.

    • Mount the volume using the mount command, e.g., sudo mount /dev/xvdf /mnt/ebs.

  4. Repeat for the Other Instance: Perform the same steps to mount the EBS volume on the second instance.

Conclusion 🎯

Congratulations! You have successfully created an EBS volume and attached it to multiple EC2 instances. This setup is particularly useful when you need shared storage between instances, such as in a clustered or high-availability environment.

To summarize, we:

  • Created two EC2 instances.

  • Set up an EBS volume with multi-attach capability.

  • Attached the volume for both instances.

  • Mounted the volume on each instance to make it usable.

By understanding these fundamental steps, you can now leverage AWS's EBS volumes more effectively in your cloud infrastructure. Whether you're setting up a database cluster or a distributed application, the ability to share storage across instances is a powerful feature that can enhance your application's reliability and scalability.

You can Buy Me a Coffee if you want to and don't forget to follow me on YouTube, Twitter, and LinkedIn.

If you have any questions or need further clarification, feel free to reach out. Happy cloud computing!

Β