Create and Attach EBS Volumes

Authors
  • avatar
    Name
    Michael Bui
    Twitter

Overview

EBS volumes are block level storage devices that can attach to instances. You can use these volumes similar to how you would use a physical hard drive. EBS volumes must be in the same availability zone of the EC2 instance it attaches to.

In this lab, we'll create an EBS volume & attach it to an EC2 instance. We'll then detach it from the EC2 instance and connect it to another EC2 instance.

Creating the EBS Volume

  1. From EC2 go to Volumes -> Create Volume
  2. Give the volume a size - 8 GiB
  3. Select the availabiliy zone this EBS Volume will reside in - us-west-2a
  4. Create the volume

Attaching to EC2 Instances

  • We have 3 instances: Lab-1, Lab-2, and Lab-3
  • Lab-1 and Lab-2 are in availability zone 2a & Lab-3 is in availability zone 2b
  1. We'll first attach the EBS Volume to Lab-1
  2. Navigate to the EBS volume and select Attach volume
  3. Select an instance to attach the volume to
  4. Notice that Lab-3 is not listed because it is not running in the same availability zone as the EBS volume

Verify EBS Volume is Attached

  1. SSH into the Lab-1 Instance
  2. Use lsblk command to list disks available on the EC2 instance
  3. The attached EBS volume is /dev/xvdf which contains no partitions

Using the EBS Volume

Documentation for making EBS volumes available for use here

  • New volumes are raw block devices and need a file system before we can mount and use them
  1. Format the EBS volume as xfs by running sudo mkfs -t xfs /dev/xvdf
  2. Mount the volume
  • Create a directory with
    sudo mkdir /data
    
  • Mount with
    sudo mount /dev/xvdf /data
    
  1. Go into /data and create some files

Moving EBS Volume

  1. Go into the EBS Volume and select Detach volume
  2. We'll now reattach the volume to Lab-2
  3. Inside Lab-2 we can run lsblk to see our attached volumes
  4. Create a mount point with sudo mkdir /data and mount the volume
  5. Inside our Lab-2 instance we see that the files we made when the EBS volume was attached to Lab-1 have carried over