Amazon Elastic Block Store (EBS) provides highly available and reliable block-level storage volumes for use with Amazon EC2 instances. EBS volumes allow you to scale storage based on your application needs, detach from one instance and attach to another, take snapshots for backup and disaster recovery, as well as monitor and adjust performance.
In this comprehensive 2600+ word guide, we will cover everything a full-stack developer needs to know about mounting EBS volumes to EC2 instances, from creating volumes to attaching them, configuring filesystems, mounting, performance optimization, backups and more.
Overview of EBS Volume Types
There are several types of EBS volumes optimized for different use cases. The volume type determines the underlying performance characteristics:
- General Purpose SSD (gp2) – Default go-to option for most workloads. Balance of price and performance with 3 IOPS per GB and up to 16000 max IOPS per volume.
- Provisioned IOPS (io1) – Highest performance SSD option designed for mission-critical transactional workloads. Allows provisioning up to 64000 IOPS per volume.
- Throughput Optimized HDD (st1) – Lower cost HDD volumes for frequently accessed throughput-intensive workloads. Up to 500MB/s throughput per volume.
- Cold HDD (sc1) – Lowest cost storage best suited for infrequently accessed data. Can scale to petabytes of data.
- Magnetic Standard – Legacy HDD option with lowest IOPS performance.
General Purpose SSD and Provisioned IOPS are the most common choices where consistent performance is critical. The ability to tailor volumes based on workload patterns is a key benefit of EBS vs using the ephemeral storage that comes with an EC2 instance.
Comparing EBS vs Instance Store Volumes
The alternative to using EBS volumes is to instead leverage the temporary block-level instance storage that comes with some EC2 instance types. Unlike EBS volumes which are network-attached disks, instance store volumes are physically connected to the host server.
Instance store volumes have higher peak IOPS performance potential given the direct attached storage. However, the drawbacks tend to outweigh this benefit for most production workloads:
- Data is lost when the EC2 instance is stopped or terminated
- No ability to detach and re-attach to other instances
- No ability to scale capacity beyond the instance limits
- No backup snapshots or redundancy options
- Instance choices limited to storage optimized instance types
EBS volumes provide better data durability, flexibility, scalability and availability compared to instance store devices in most cases.
Creating and Attaching EBS Volumes
We covered the basics of creating and attaching EBS volumes using the AWS console earlier. The key steps include:
- Create volume – Select type, size up to 16 TiB, and availability zone
- Attach volume – Select EC2 instance and mount point
- Create filesystem – XFS, Ext4, etc to interface with OS
- Mount the formatted volume to a directory location
Programmatically, this same process can be automated using AWS CLI or SDK calls for infrastructure-as-code workflows:
# Create new GP2 volume
aws ec2 create-volume --size 500 --region us-east-1 --volume-type gp2
# Attach to instance i-12345abcd
aws ec2 attach-volume --volume-id vol-123abc --instance-id i-12345abcd --device xvdf
From an architectural viewpoint, a best practice is to separate boot volumes from scratch data volumes allowing easier resource scaling and backup management.
Filesystem Creation Options
Once an EBS volume has been attached at the block device level to an EC2 instance, we need to create a filesystem that the operating system can use to interact with files stored on the disk.
Some common filesystem options include:
- XFS: High performance 64-bit journaling filesystem optimized for parallel I/O. Good general purpose option.
- Ext4: Widely popular Linux filesystem with good overall performance. Limits maximum size to 50 TB.
- Btrfs: More advanced CoW filesystem allowing easy snapshots and management. Still maturing stability-wise.
Our previous example demonstrated using mkfs to create a XFS partition, but any of these options are freely interchangeable:
# XFS filesystem
sudo mkfs -t xfs /dev/xvdf
# Ext4 filesystem
sudo mkfs -t ext4 /dev/xvdf
# Btrfs filesystem
sudo mkfs -t btrfs /dev/xvdf
The primary factors are matching the performance characteristics of your workload and using a filesystem with stability appropriate for your risk tolerance policies.
Mounting the EBS Volume
Mounting the prepared EBS volume makes it available within the standard Linux directory structure for file operations.
We mount volumes using the mount command, passing the block device path and desired mount location:
# Mount device /dev/xvdf to path /mnt
sudo mount /dev/xvdf /mnt
Common mount points include /mnt, /opt or /data but this can be configured as needed.
To ensure mount permissions allow access for the ec2-user, we specify the uid/gid options:
# Set permissions for ec2-user
sudo mount -o uid=1000,gid=1000 /dev/xvdf /mnt
And customize read/write access:
# Mount as read-only
sudo mount -o ro /dev/xvdf /mnt
# Mount with no exec on volume
sudo mount -o noexec /dev/xvdf /mnt
Testing Volume Access
Once a volume has been mounted, standard Linux file commands can interact with the new storage space:
user@server:~$ df -h
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/xvdf 10205324 Cupido_dolor 10433020 1% /mnt
user@server:~$ touch /mnt/file.txt
user@server:~$ ls /mnt
file.txt
This confirms we have read-write access as expected.
Benchmarking and Monitoring Performance
A key advantage of EBS volumes compared to instance stores is the ability to scale performance as needed using provisioned IOPS. Monitoring metrics like IOPS, throughput, latency, queue depth, and burst credit balance is critical to optimizing performance.
The AWS CLI get-volume-stats command returns a snapshot of performance data:
aws ec2 get-volume-stats --volume-id vol-123abcd
Results include metrics like:
{
"VolumeId": "vol-123abcd",
"Size": 100,
"SnapshotId": "snap-a1b2c3d4",
...
"VolumeStats": {
"VolumeIdleTime": 23,
"VolumeReadOps": 1550,
"VolumeWriteOps": 822,
"VolumeReadBytes": 1024000,
"VolumeWriteBytes": 512000,
"VolumeQueueLength": 3,
"VolumeThroughputPercentage": 0.55,
"VolumeConsumedReadWriteOps": 1000
},
...
}
Based on your application needs, you can tune EBS IOPS and volume sizes appropriately. Provisioned IOPS volumes allow setting precise IOPS targets between 100 and 64000 (at 50 IOPS per GiB).
There are several AWS services that help establish performance baselines and optimize instances & EBS:
- EC2 Fleet can launch heterogeneous instances helped by automatic modeling of performance needs
- EC2 Auto Scaling responds to changes in demand dynamically
- EBS Elastic Volumes scale capacity without disrupting applications
Combining robust monitoring data with predictive analytics and automation helps attain steady state utilization that maximizes EBS investments.
Resizing EBS Volumes
EBS makes it easy to scale storage on the fly to meet changing workload requirements. Volumes can be modified without detaching them from instances they are currently attached to (unlike traditional block storage).
Using AWS console or CLI, you can easily resize the EBS volume capacity down or upwards for gp2, io1 and io2 volume types.
To take advantage of a resized EBS volume, the filesystem itself needs to be resized using utilities like:
- XFS:
xfs_growfs /mountpoint - Ext4:
resize2fs /dev/xvdf
For example, let‘s grow an EBS volume from 100GiB to 500GiB, verify increased raw space and resize XFS:
user@host:~$ df -h /ebs
Filesystem Size Used Avail Use% Mounted on
/dev/xvdf 99G 45G 55G 45% /ebs
# Modify volume size to 500G
aws ec2 modify-volume --volume-id vol-123abcd --size 500
# Verify new size
user@host:~$ lsblk
xvdf 500G
# Resize XFS filesystem
sudo xfs_growfs -d /ebs
user@host:~$ df -h /ebs
Filesystem Size Used Avail Use% Mounted on
/dev/xvdf 500G 45G 455G 9% /ebs
The filesystem is now expanded allowing more data storage without disruption. This on-demand scaling enables applications to leverage cloud economies of scale.
Cloning EBS Volumes with Snapshots
Critical for backup/recovery use cases is creating periodic EBS snapshots to S3. These incremental compressed snapshots allow restoring volumes or cloning new ones from any point-in-time.
Creating manual or automated snapshots is easy through AWS console or CLI:
# Create snapshot
aws ec2 create-snapshot --volume-id vol-123abcd
# View all snapshots
aws ec2 describe-snapshots
From a snapshot, we can create an identical volume or build new volumes with different configs:
# Create clone from snapshot
aws ec2 create-volume
--snapshot-id snap-a1b2c3d4
--size 100
--volume-type io1
--iops 300
Snapshots enable replicating production volumes to dev/test environments for application testing needs. Combined with encryption, snapshots guard against data loss scenarios.
To rotate backups on a schedule, lifecycle manager rules can transition previous point-in-time snaps to cold storage classes cost-effectively. This can be paired with DataSync jobs to offload snapshots to on-prem for disaster recovery.
Overall EBS + EFS is typically lower TCO for block storage compared to DIY SAN scale-out solutions – with less headache maintaining it all.
Detaching Volumes and Unmounting
Before you can detach an EBS volume from an EC2 instance, you first need to unmount the device from the operating system which syncs all data and updates metadata:
# Unmount /mnt
sudo umount /mnt
Once unmounted cleanly, it is safe to detach the volume through AWS console or CLI without losing data:
# Detach EBS volume
aws ec2 detach-volume --volume-id vol-123abcd
This can allow moving datasets between instances and Availability Zones as needed.
Best Practices and Takeaways
Here are some key best practices when working with EBS volumes:
- Use GP2 for most workloads as a cost-effective blend of price/performance
- Leverage provisioned IOPS when low latency and consistency is critical
- Place instances and volumes inside the same AZ for maximum IOPS potential
- Monitor volume metrics like IOPS, throughput, latency to optimize
- Resize without detaching to scale storage dynamically
- Schedule periodic snapshots for backups and cross-region DR
- Lifecycle snapshot policies to Glacier to reduce costs for archives
- Encrypt volumes & snapshots for security against data theft
EBS provides versatile building blocks for high performance storage tiering in AWS. By following recommendations around volume types, rightsizing, monitoring and automation, applications can scale on-demand while keeping data safe and resilient.
Conclusion
Elastic Block Store volumes provide high performance, resilient and scalable block-level storage customized for a variety of workload profiles. With the ability to dial IOPS up and down based on metrics and application needs, instances can be purpose built for superior economics. Snapshots guard data integrity while native encryption secures assets against unauthorized access. By managing capacity, performance and backups judiciously, production systems as well as data lakes can thrive safely, efficiently and affordably within the AWS ecosystem thanks to services like EBS.


