Working with Amazon Elastic Block Store (Amazon EBS)

Working with Amazon Elastic Block Store (Amazon EBS)

This particular tutorial centers on Amazon Elastic Block Store (Amazon EBS), a crucial storage mechanism for Amazon EC2 instances. Throughout the lab, you will gain insights into creating an Amazon EBS volume, linking it to an instance, implementing a file system on the volume, and eventually generating a snapshot backup.

Key Topics Covered:

  1. Creating an Amazon EBS volume

  2. Attaching and mounting the volume to an EC2 instance

  3. Creating a snapshot of the volume

  4. Generating a new volume from the snapshot

  5. Attaching and mounting the new volume to the EC2 instance

Lab Pre-requisites:

To successfully complete this lab, it is recommended that you have a basic understanding of Amazon EC2 usage and fundamental Linux server administration. Familiarity with the Linux command-line tools is essential.

Duration:

Expect to spend approximately 30 minutes completing this lab.

Understanding Amazon Elastic Block Store:

Amazon Elastic Block Store (Amazon EBS) provides persistent storage for Amazon EC2 instances. EBS volumes are network-attached and persist independently of an instance's lifecycle. Highly available and reliable, these volumes can function as a boot partition or be attached to a running EC2 instance as a standard block device.

EBS Volume Features:

  • Persistent storage: Volume lifetime is independent of any specific EC2 instance.

  • General purpose: Raw, unformatted block devices usable from any operating system.

  • High performance: Comparable to or better than local EC2 drives.

  • High reliability: Built-in redundancy within an Availability Zone.

  • Designed for resiliency: Annual Failure Rate (AFR) of Amazon EBS ranges between 0.1% and 1%.

  • Variable size: Volume sizes ranging from 1 GB to 16 TB.

  • Easy to use: Simple creation, attachment, backup, restoration, and deletion of Amazon EBS volumes.

The lab guide provides a step-by-step overview of basic Amazon EBS concepts, but for more in-depth information, refer to the Amazon EBS documentation.

Task 1: Create a New EBS Volume

In this task, you will create and attach an Amazon EBS volume to a new Amazon EC2 instance.

Steps:

  1. Access the AWS Management Console, and navigate to the Services menu. Click on EC2.

  2. In the left navigation pane, select Instances. Please note that an Amazon EC2 instance named Lab has already been launched for your lab.

  3. Take note of the Availability Zone of the instance, which will resemble something like us-east-1a.

  4. In the left navigation pane, click on Volumes. You will observe an existing volume currently in use by the Amazon EC2 instance. This volume has a size of 8 GiB, distinguishing it from the upcoming 1 GiB volume you are about to create.

  5. Choose Create volume and proceed to configure the following:

    • Volume Type: General Purpose SSD (gp2)

    • Size (GiB): 1. NOTE: There might be restrictions on creating larger volumes.

    • Availability Zone: Select the same availability zone as your EC2 instance.

    • Choose Add Tag.

    • In the Tag Editor, input:

      • Key: Name

      • Value: My Volume

  6. Click on Create Volume. Your new volume will be visible in the list and will transition from the Creating state to the Available state. You may need to select refresh to view your new volume.

Task 2: Attach the Volume to an Instance

Now, proceed to attach your newly created volume to the Amazon EC2 instance.

Steps:

  1. Select the volume named My Volume.

  2. In the Actions menu, opt for Attach volume.

  3. In the Instance field, select the instance listed as Lab.

  4. Take note that the Device field is configured to /dev/sdf. This device identifier will be utilized in a subsequent task.

  5. Click on Attach volume. The volume state will now reflect as In-use.

Task 3: Connect to Your Amazon EC2 Instance

For Windows Users: Using SSH to Connect

  1. Before starting, carefully read the three bullet points in this step. You won't be able to see these instructions when the Details panel is open.
  • Choose the Details dropdown menu above these instructions, then select Show. A Credentials window will open.

  • Click on Download PPK and save the labsuser.ppk file to your Downloads directory.

  • Close the Details panel by choosing the X.

  1. Download the necessary software.
  • Utilize PuTTY for SSH connections to Amazon EC2 instances. If not already installed, download it here.
  1. Open putty.exe.

  2. Configure PuTTY to avoid timeouts:

  • Choose Connection.

  • Set Seconds between keepalives to 30 to keep the PuTTY session open for a longer period.

  1. Configure your PuTTY session:
  • Choose Session.

  • Enter the Host Name (or IP address) by copying and pasting the IPv4 Public IP address for the instance from the EC2 Console.

  • In PuTTY's Connection list, expand SSH.

  • Choose Auth (don't expand it).

  • Choose Browse and select the labsuser.ppk file.

  • Click Open to select it.

  • Choose Open.

  1. Select Yes to trust the host and connect.

  2. When prompted login as, enter: ec2-user. This establishes a connection to the EC2 instance.

  3. Windows Users: Proceed to the next task.

For macOS and Linux Users

  1. Before starting, carefully read all instructions in this step. You won't be able to see these instructions when the Details panel is open.
  • Choose the Details dropdown menu above these instructions, then select Show. A Credentials window will open.

  • Click on Download and save the labsuser.pem file.

  • Close the Details panel by choosing the X.

  1. Open a terminal window and change directory (cd) to where the labsuser.pem file was downloaded. For instance:
cd ~/Downloads
  1. Change the key permissions to be read-only:
chmod 400 labsuser.pem
  1. Return to the AWS Management Console, go to the EC2 service, and select Instances. Ensure the Lab instance is selected.

  2. In the Details tab, copy the Public IPv4 address value.

  3. Return to the terminal window and run this command (replace <public-ip> with the actual public IP address you copied):

ssh -i labsuser.pem ec2-user@<public-ip>
  1. Type yes when prompted to allow the first connection to this remote SSH server. As a key pair is used for authentication, no password is required.

Task 4: Create and Configure Your File System

In this task, you will add the new volume to a Linux instance as an ext3 file system under the /mnt/data-store mount point.

  1. View the available storage on your instance:
df -h

You should see output similar to the original 8GB disk volume. Your new volume is not yet displayed.

  1. Create an ext3 file system on the new volume:
sudo mkfs -t ext3 /dev/sdf
  1. Create a directory for mounting the new storage volume:
sudo mkdir /mnt/data-store
  1. Mount the new volume:
sudo mount /dev/sdf /mnt/data-store

To configure the Linux instance to mount this volume on startup, add a line to /etc/fstab.

echo "/dev/sdf   /mnt/data-store ext3 defaults,noatime 1 2" | sudo tee -a /etc/fstab
  1. View the configuration file to see the setting on the last line:
cat /etc/fstab
  1. View the available storage again:
df -h

The output will now include an additional line - /dev/xvdf:

Filesystem Size Used Avail Use% Mounted on

/dev/xvdf 976M 1.3M 924M 1% /mnt/data-store

  1. On your mounted volume, create a file and add some text to it:
sudo sh -c "echo some text has been written > /mnt/data-store/file.txt"
  1. Verify that the text has been written to your volume:
cat /mnt/data-store/file.txt

Task 5: Create an Amazon EBS Snapshot

In this task, you will create a snapshot of your EBS volume.

  1. In the AWS Management Console, navigate to Volumes and select My Volume.

  2. In the Actions menu, choose Create snapshot.

  3. Choose Add tag and configure:

  • Key: Name

  • Value: My Snapshot

  • Choose Create snapshot.

  1. In the left navigation pane, select Snapshots. Your snapshot will initially have a state of Pending, indicating creation. It will eventually change to a state of Completed.
  • Note: Only used storage blocks are copied to snapshots, saving storage space for empty blocks.
  1. In your remote SSH session, delete the file created on your volume:
sudo rm /mnt/data-store/file.txt
  1. Verify that the file has been deleted:
ls /mnt/data-store/

Task 6: Restore the Amazon EBS Snapshot

If you ever wish to retrieve data stored in a snapshot, you can restore the snapshot to a new EBS volume.

Create a Volume Using Your Snapshot

  1. In the AWS Management Console, select My Snapshot.

  2. In the Actions menu, choose Create volume from snapshot.

  3. For Availability Zone, select the same availability zone used earlier.

  4. Choose Add tag and configure:

  • Key: Name

  • Value: Restored Volume

  • Choose Create volume.

  • Note: When restoring a snapshot to a new volume, you can also modify the configuration, such as changing the volume type, size, or Availability Zone.

Attach the Restored Volume to Your EC2 Instance

  1. In the left navigation pane, select Volumes.

  2. Choose Restored Volume.

  3. In the Actions menu, choose Attach volume.

  4. In the Instance field, select the (Lab) instance that appears. Note that the Device field is set to /dev/sdg—you will use this device identifier in a later task.

  5. Choose Attach volume. The volume state is now in-use.

Mount the Restored Volume

  1. Create a directory for mounting the new storage volume:
sudo mkdir /mnt/data-store2
  1. Mount the new volume:
sudo mount /dev/sdg /mnt/data-store2
  1. Verify that the volume you mounted contains the file created earlier:
ls /mnt/data-store2/

Conclusion

Congratulations! You have successfully:

  • Created an Amazon EBS volume

  • Attached the volume to an EC2 instance

  • Created a file system on the volume

  • Added a file to the volume

  • Created a snapshot of your volume

  • Created a new volume from the snapshot

  • Attached and mounted the new volume to your EC2 instance

  • Verified that the file created earlier is on the newly created volume.