Sub topic 15
Sharing a volume with Multi-Attach
This chapter assumes you can create and attach EBS volumes, and that you know how provisioned IOPS volumes (io1 and io2) work. It also assumes you are comfortable with Linux file systems such as XFS and ext4. Multi-Attach changes one basic rule of EBS: a volume normally belongs to a single instance, and with Multi-Attach several instances can use it at once. This sounds convenient and is easy to misuse.
What Multi-Attach does
Multi-Attach lets you attach one Provisioned IOPS SSD volume to up to 16 EC2 instances in the same Availability Zone. Every attached instance gets full read and write access to the volume. Each instance sees the volume as an ordinary block device, for example /dev/nvme1n1.
EBS does not coordinate the writers. It exposes the same blocks to every instance and nothing more. Locking, ordering of writes and cache consistency are your responsibility, either in a cluster-aware file system or in the application.
The following diagram shows the arrangement.
Requirements and limits
The main constraints are:
| Constraint | Detail |
|---|---|
| Volume type | io1 or io2 only. gp3, gp2, st1 and sc1 do not support Multi-Attach. |
| Instances | Nitro-based instances only, up to 16 per volume. |
| Availability Zone | The volume and every instance must be in the same AZ. |
| Root volumes | A Multi-Attach volume cannot be a boot volume. |
| Region support | io1 Multi-Attach is available only in some Regions. Check the current AWS documentation for your Region. |
| Encryption | Supported. All attached instances use the same KMS key transparently. |
Multi-Attach does not give you redundancy across Availability Zones. If the AZ has a problem, every instance sharing the volume is affected.
For io2 volumes, you can enable or disable Multi-Attach after creation with modify-volume, provided the volume is attached to at most one instance. For io1, you must decide at creation time. Verify this against the current documentation, because the rules have changed as the feature has expanded.
Creating and attaching a shared volume
Create the volume with the --multi-attach-enabled flag. This example uses io2 with 10,000 provisioned IOPS.
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type io2 \
--size 100 \
--iops 10000 \
--multi-attach-enabled \
--encrypted \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=shared-vol}]'Attach it to each instance with a separate attach-volume call. Use the same volume ID and a different instance ID each time.
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdf
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0fedcba9876543210 \
--device /dev/sdfTo see every attachment and confirm the setting, query the volume.
aws ec2 describe-volumes \
--volume-ids vol-0123456789abcdef0 \
--query 'Volumes[0].{MultiAttach:MultiAttachEnabled,Attachments:Attachments[].{Instance:InstanceId,State:State,DeleteOnTermination:DeleteOnTermination}}'Each attachment has its own state and its own DeleteOnTermination flag.
The file system problem
This is the mistake that causes data loss. Do not format a Multi-Attach volume with XFS or ext4 and mount it read-write on several instances.
Those file systems assume one kernel owns the volume. Each instance caches metadata, such as inodes, directory entries and free-space maps, in its own memory. When two instances write through different caches, they overwrite each other's metadata, and the file system becomes corrupt. EBS does not detect this.
You have three safe options:
- A cluster-aware file system. Examples are GFS2 or OCFS2, together with a cluster manager such as Pacemaker and Corosync that provides fencing and a distributed lock manager. This is the standard route for shared-disk file systems.
- An application that manages raw block access. Some clustered databases and storage engines divide the device into regions and coordinate access themselves.
- A single writer with read-only readers. One instance mounts read-write and the others mount read-only. Readers still cache stale data, so this suits only data that changes rarely, and it is fragile.
Write fencing with NVMe reservations
Clustered systems need a way to stop a failed or unresponsive node from writing, which is called fencing. io2 volumes on supported Nitro instances support NVMe reservations, which implement the persistent reservations model found in SCSI and NVMe shared storage. A cluster manager registers each node, acquires a reservation on the volume, and can then preempt a failed node so that its writes are rejected.
You can inspect reservation support from a Linux instance with nvme-cli, for example sudo nvme resv-report /dev/nvme1n1. In practice, cluster tooling such as Pacemaker's fencing agents drives these operations, and you rarely call them by hand. Check that your instance type and volume type support reservations before designing around them, because io1 volumes do not.
Performance behavior
Provisioned IOPS and throughput belong to the volume, not to each instance. If you provision 10,000 IOPS, all attached instances together share that 10,000. Adding instances does not add performance.
Each instance also has its own EBS bandwidth and IOPS limit, which applies to all of that instance's volumes. The effective rate for one instance is the lower of the volume's remaining capacity and the instance's limit. Chapter material on EBS-optimized instances applies here unchanged.
Two practical consequences follow:
- Size the provisioned IOPS for the sum of the expected load from all nodes, plus some headroom.
- Watch each instance's
VolumeReadOpsandVolumeWriteOpsas well as the volume-level metrics. CloudWatch publishes these per volume, so a total near the provisioned figure means the nodes are competing for the same budget.
Detaching, deleting and snapshots
Detaching works per instance. Unmount the device inside the guest first, then call detach-volume with the instance ID.
aws ec2 detach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0If the volume has more than one attachment, include --instance-id, so you detach only the instance you intend.
On termination, a Multi-Attach volume is deleted only when the last attached instance terminates, and only if that attachment has DeleteOnTermination set to true. Terminating the other instances leaves the volume in place. Because the setting is per attachment, check it explicitly if you need the volume to survive.
Snapshots work as usual. A snapshot of a shared volume is crash-consistent: it captures the blocks as they were at the moment of the snapshot, but not data still in the instances' memory caches. For a cluster-aware file system, quiesce the application on all nodes first if you need application consistency.
Common errors
- Attach fails with an incompatibility error. The instance is not Nitro-based, is in a different AZ, or the volume does not have Multi-Attach enabled.
- Cannot enable Multi-Attach on an existing volume. The volume is io1, or it is an io2 volume with more than one attachment.
- File system corruption after a reboot or failover. A non-cluster file system was mounted read-write on more than one node, or fencing failed and a stale node kept writing.
- Lower throughput than expected. The nodes together exceed the provisioned IOPS, or one instance reached its own EBS limit.
Exercise
You provision an io2 Multi-Attach volume with 20,000 IOPS and attach it to four instances. A colleague expects 80,000 IOPS in total. Is the colleague right? What else must be true before the four instances can safely write to the volume at once?
Show answer
No. The 20,000 IOPS is a volume-level limit shared by all four instances, so the total is at most 20,000, and each instance is also capped by its own EBS limit. For safe concurrent writes, the instances must be Nitro-based and in the same AZ as the volume, and the volume must use a cluster-aware file system (such as GFS2 or OCFS2, with a cluster manager and fencing) or an application that coordinates block access itself. XFS or ext4 mounted read-write on all four would corrupt the data.
Exercise
Which command output tells you whether a volume has Multi-Attach enabled and which instances currently hold it? What happens to the volume if one of three attached instances is terminated?
Show answer
aws ec2 describe-volumes --volume-ids <id> returns MultiAttachEnabled and an Attachments list with one entry per instance. Terminating one of three instances removes only that attachment, and the volume stays. The volume is deleted only when the last attached instance terminates and that attachment has DeleteOnTermination set to true.