Skip to main content

Command Palette

Search for a command to run...

Boost EC2 Storage: Create and Attach EBS Volumes Step-by-Step

Updated
β€’5 min read

When you launch an EC2 instance, AWS automatically creates a root volume.
If your application needs more storage, you can:

  • Add a volume during launch

  • Or attach an EBS volume later (this lab covers that)

Step 1: Launch an EC2 Instance

  1. Go to Amazon Web Services Console

  2. Navigate to Amazon EC2

  3. Click Launch Instance

  4. Configure:

    • AMI (e.g., Ubuntu)

    • Instance type (e.g., t2.micro)

    • Key pair

  5. Launch the instance

πŸ‘‰ A default root EBS volume is created automatically.

To add an additional volume during instance creation, select "Add New Volume" in the configuration step.

Step 2: To create an Additional EBS Volume

  1. Go to EBS β†’ Volumes

  2. Click Create Volume

  3. Configure:

    • Size (e.g., 10 GB)

    • Volume type (gp3 recommended)

⚠️ IMPORTANT: Select the same Availability Zone as your EC2 instance

Click Create Volume

Step 3: Attach Volume to EC2

  1. Select the newly created volume

  2. Click Actions β†’ Attach Volume

  3. Choose your EC2 instance

  4. Click Attach

Step 4: Connect to EC2 Instance

Use SSH:

ssh -i your-key.pem ubuntu@your-public-ip

Step 5: Detect Attached Volume

List block devices:

lsblk

Shows:

  • All block devices detected by OS

  • Even unmounted volumes

Example volume names:

  • /dev/nvme0n1 β†’ root volume

  • /dev/nvme1n1 β†’ additional volume

Check mounted file systems:

df -Th

Shows:

  • Mounted volumes only

  • File system type

  • Human-readable sizes

From the df -Th output, you'll notice that the new volume (e.g., /dev/nvme1n1) is not listed because it hasn't been mounted yet. Once mounted, it will appear.

Step 6: Verify File System Presence

sudo file -s /dev/nvme1n1

Output Meaning:

  • data β†’ No filesystem exists

  • XFS / ext4 β†’ Filesystem already exists

file -s /dev/nvme1n1

From this output, "data" indicates that no file system is present. To mount the volume, first create an XFS file system.

7: Create XFS File System

πŸ‘‰ One-time activity

sudo mkfs -t xfs /dev/nvme1n1

Verify:

sudo file -s /dev/nvme1n1

Verify the output: It displays as an XFS file system.

8: Create Mount Directory

First, create a directory that will act as the mount point for your EBS volume:

mkdir -p /home/ubuntu/production

πŸ“„ Create a Sample File

Create a test file inside the directory:

This file will help verify that data written to this directory is stored on the attached EBS volume.

touch /home/ubuntu/production/abc.logs

The /home/ubuntu/production directory will be used as the mount point for the EBS volume in Amazon EC2.

Once the volume is mounted:

  • Any data written inside this directory will be stored on the Amazon Elastic Block Store volume

  • This ensures data persistence, even if the EC2 instance is stopped or terminated (depending on volume settings)

πŸ‘‰ This is a common real-world use case of EBS for storing logs, application data, or backups.

9: Mount the Volume (Temporary)

sudo mount /dev/nvme1n1 /home/ubuntu/production

Verify:

df -Th

πŸ‘‰ Volume is now mounted

Step 10: Make Mount Permanent

Temporary mounts are stored in:

sudo cat /etc/mtab

Get last entry:

tail -1 /etc/mtab

Copy the last line

Edit fstab:

sudo vi /etc/fstab

Add the last line you copied above.

Step 11: Test Persistence

Reboot:

Now, even if you reboot the server, the volume should remain mounted. βœ…

12: Increase the EBS volume

  • You can increase EBS volume size anytime

  • You cannot decrease volume size

As you can see, I can't decrease the size, but I can increase it.

Expand File System (Linux – XFS)

When you increase the size of an EBS volume in Amazon Elastic Block Store, the operating system does not automatically use the extra space.

πŸ‘‰ You must manually expand the filesystem to utilize the new capacity.

Step 1: Verify Current Disk Size

Check the current disk and mount details:

lsblk
df -Th

πŸ‘‰ You may notice:

  • Disk size increased (from AWS side)

  • Filesystem size still unchanged

sudo xfs_growfs -d /home/ubuntu/production
  • xfs_growfs β†’ Expands an XFS filesystem

  • -d β†’ Uses all available free space on the disk

  • /home/ubuntu/production β†’ Mount point of the volume

πŸ‘‰ This command resizes the filesystem without unmounting

Verify Expansion

df -Th

πŸ‘‰ Now you should see:

  • Increased total size

  • More available space

Important Notes

  • βœ” Only works if filesystem type is XFS

  • βœ” Volume must already be mounted

  • βœ” Safe operation (no data loss)

  • ❌ Cannot shrink XFS filesystem

Key Concepts Learned

  • EBS works at block level storage

  • Volume must be in same Availability Zone

  • Mounting connects storage to filesystem

  • UUID ensures stable mounting

  • XFS supports online expansion

Real-World Use Case

Mounting /home/ubuntu/production ensures:

  • Logs and application data are stored on EBS

  • Data persists even if EC2 instance is stopped or replaced

  • Ideal for production workloads

Conclusion

You now have a complete understanding of:

  • Managing storage in AWS

  • Attaching and mounting EBS volumes

  • Ensuring persistence

  • Scaling storage without downtime

πŸ‘‰ This is a core DevOps skill used in real production environments.