Try EBS Multi-Attach

Try EBS Multi-Attach

One EBS can be attached to multiple EC2 instances if certain conditions are met.

In this article, we will examine EBS multi-attachment.

First of all, please note that this verification did not yield any meaningful results.
Please understand this in advance.

Environment

Diagram of tring EBS Multi-Attach

Create two EC2 instances.
The instances will be the latest Amazon Linux 2.

Create an EBS.
Enable EBS multi-attach.

Create endpoints for SSM for two purposes.
The first purpose is to run the SSM document and perform the initialization process for the instance. Specifically, EBS attach, etc.
The second purpose is to access the instance with SSM Session Manager.

CloudFormation Template Files

The above configuration is built with CloudFormation.
The CloudFormation templates are located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/095

Explanation of key points of the template files

This page covers EBS multi-attachment.

For basic information on EBS, please refer to the following page

https://awstut.com/en/2021/12/12/attaching-ebs-to-linux-instance

EBS

Resources:
  EBS:
    Type: AWS::EC2::Volume
    Properties:
      AvailabilityZone: !Sub "${AWS::Region}${AvailabilityZone}"
      Iops: !Ref EBSIops
      MultiAttachEnabled: true
      Size: !Ref EBSVolumeSize
      VolumeType: !Ref EBSVolumeType

  EBSAttachment1:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      Device: !Sub "/dev/sd${DeviceNameSuffix}"
      InstanceId: !Ref Instance1
      VolumeId: !Ref EBS

  EBSAttachment2:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      Device: !Sub "/dev/sd${DeviceNameSuffix}"
      InstanceId: !Ref Instance2
      VolumeId: !Ref EBS
Code language: YAML (yaml)

There are several restrictions to enable EBS multi-attachment.
We will cover the most important ones below.

  • The EBS and instance must be located on the same AZ.
  • The EBS must be a provisioned IOPS SSD (io1 and io2) volume.

There are many other restrictions, please check the official AWS page.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-volumes-multi.html

In the MultiAttachEnabled property, specify to enable EBS multi-attach.
In this case, we will enable it, so set “true” to this property.

Other parameters are summarized below.

  • Volume type: io2
  • IOPS: 100
  • Volume size: 4GB

(Reference) SSM association

Resources:
  RunShellScriptAssociation:
    Type: AWS::SSM::Association
    Properties:
      AssociationName: !Sub "${Prefix}-run-shellscript-association"
      Name: AWS-RunShellScript
      OutputLocation:
        S3Location:
          OutputS3BucketName: !Ref LogBucket
          OutputS3KeyPrefix: !Sub "${Prefix}/run-powershellscript-association"
      Parameters:
        commands:
          - !Sub |
              mount_point="${MountPoint}"
              file_system="${FileSystem}"

              for d in $(lsblk -n -r -p | grep disk | awk '{ print $1 }' )
              do
                if  [ "$(sudo file -s -b $d)" == data ]; then
                  sudo mkfs -t $file_system $d
                fi

                if [[ "$(sudo file -s -b $d | tr '[:upper:]' '[:lower:]')" =~ $file_system ]]; then
                  sudo mkdir $mount_point
                  sudo mount $d $mount_point

                  uuid=$(sudo blkid $d -o export | grep ^UUID)
                  echo -e "$uuid\t/data\t$file_system\tdefaults,nofail\t0\t2" >> /etc/fstab
                fi
              done
      Targets:
        - Key: InstanceIds
          Values:
            - !Ref Instance1
            - !Ref Instance2
      WaitForSuccessTimeoutSeconds: !Ref WaitForSuccessTimeoutSeconds
Code language: YAML (yaml)

Execute SSM document AWS-RunShellScript to perform the instance initialization process.
The contents to be executed are as follows

  • Create a file system (xfs) if the EBS has no file system created yet.
  • If there is an xfs type file system device, create a mount point and mount it.
  • Add a note to /etc/fstab for automatic mounting.

Architecting

Use CloudFormation to build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create a CloudFormation stack.
For information on how to create stacks and check each stack, please refer to the following page

https://awstut.com/en/2021/12/11/cloudformations-nested-stack

After checking the resources in each stack, information on the main resources created in this case is as follows

  • Instance 1: i-075f601c26550180b
  • Instance 2: i-08369c7e5eaac4834
  • EBS: vol-008299ff8f19b3a89

Confirm the created resource from the AWS Management Console.
Check the EBS.

Detail of EBS 1.
Detail of EBS 2.

The EBS was successfully created.
The Multi-Attach enabled item says “Yes”.
Attached Instances shows “2 attached”. Since Multi-Attach is enabled, it means that it is attached to two instances at the same time.

Check the execution history in the SSM document.

Detail of SSM Run Command 1.

The SSM document AWS-RunShellScript has been executed for both instances.
Now the file system for the EBS has been created and mounted.

Check Action

Instance 1

Now that everything is ready, we access instance 1.
SSM Session Manager is used to access the instance.

% aws ssm start-session --target i-075f601c26550180b
sh-4.2$
Code language: Bash (bash)

For more information on SSM Session Manager, please see the following page

https://awstut.com/en/2021/12/11/accessing-a-linux-instance-via-ssm-session-manager

Check the EBS mount status.

sh-4.2$ lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1       259:0    0   8G  0 disk
├─nvme0n1p1   259:1    0   8G  0 part /
└─nvme0n1p128 259:2    0  10M  0 part /boot/efi
nvme1n1       259:3    0   4G  0 disk /data
Code language: Bash (bash)

You will see that it is indeed mounted on /data.

Write the file to the EBS.

sh-4.2$ sudo touch /data/test.txt
sh-4.2$ ls /data
test.txt
Code language: Bash (bash)

The file has indeed been created.

Instance 2

Next, access instance 2.

% aws ssm start-session --target i-08369c7e5eaac4834
sh-4.2$
Code language: Bash (bash)

Check the EBS mount status.

sh-4.2$ lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1       259:0    0   8G  0 disk
├─nvme0n1p1   259:1    0   8G  0 part /
└─nvme0n1p128 259:2    0  10M  0 part /boot/efi
nvme1n1       259:3    0   4G  0 disk /data
Code language: Bash (bash)

The EBS is mounted.
As expected, one EBS is multi-attached to two instances.

Check the EBS area.

sh-4.2$ ls /data
sh-4.2$
Code language: Bash (bash)

The file just created on instance 1 does not appear.

Apparently, they will not be reflected in instance 2 unless you unmount them once.

sh-4.2$ sudo umount /data
sh-4.2$ sudo mount /dev/nvme1n1 /data
Code language: Bash (bash)

Check the EBS area again.

sh-4.2$ ls /data
test.txt
Code language: Bash (bash)

The file has been confirmed.

Thus, EBS multi-attachment is not expected to behave like EFS.

Summary

We have confirmed the behavior of EBS multi-attach.