Skip to content

Easiest way to Attach EBS volume multi-attach to Auto Scaling Group

0

Could anyone suggest the easiest way to Attach EBS volume multi-attach to Auto Scaling Group, I use this in user data, it's not work:

#!/bin/bash
# Variables
VOLUME_TAG_KEY="Purpose"
VOLUME_TAG_VALUE="MyAppData"
DEVICE_NAME="/dev/sdf"  # Update as needed

# Get instance details
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)

# Locate the correct EBS volume by tag
VOLUME_ID=$(aws ec2 describe-volumes --region $REGION --filters "Name=tag:$VOLUME_TAG_KEY,Values=$VOLUME_TAG_VALUE" "Name=status,Values=available" --query "Volumes[0].VolumeId" --output text)

# Attach volume if found
if [ "$VOLUME_ID" != "None" ]; then
    echo "Attaching volume $VOLUME_ID to $INSTANCE_ID at $DEVICE_NAME"
    aws ec2 attach-volume --region $REGION --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device $DEVICE_NAME

    # Wait until the volume is attached
    while [ "$(aws ec2 describe-volumes --volume-ids $VOLUME_ID --query "Volumes[0].Attachments[0].State" --output text)" != "attached" ]; do
        echo "Waiting for volume to attach..."
        sleep 5
    done

    # Mount the volume
    mkdir -p /data
    mount $DEVICE_NAME /data
    echo "$DEVICE_NAME /data ext4 defaults,nofail 0 2" >> /etc/fstab
    echo "Volume $VOLUME_ID attached and mounted at /data"
else
    echo "No available volume found with tag $VOLUME_TAG_KEY=$VOLUME_TAG_VALUE. Exiting."
    exit 1
fi

Actually, I would like to mount to the /dev/sda1, could anyone help?

Or map to another path is easier?

#!/bin/bash
VOLUME_ID=<your-volume-id>
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 attach-volume --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device /dev/sdf

Thank you very much.

1 Answer
2

To attach an EBS volume with multi-attach capability to an Auto Scaling Group, there are a few important considerations and steps to take:

  1. Multi-Attach is only supported for io2 volume types. Ensure your EBS volume is of this type.

  2. The volume must be created with multi-attach enabled. You can't enable it for io1 volumes after creation, but you can for io2 volumes if they're not attached to any instances.

  3. All instances in the Auto Scaling Group must be in the same Availability Zone as the multi-attach enabled volume.

  4. Instead of using user data to attach the volume, it's generally better to use lifecycle hooks with your Auto Scaling Group. This allows you to perform actions when instances are launched or terminated.

  5. Mounting to /dev/sda1 is not recommended as it's typically the root volume. It's better to use a different device name for additional volumes.

Here's a revised approach:

  1. Create your multi-attach enabled io2 volume:
aws ec2 create-volume --volume-type io2 --multi-attach-enabled --size 10 --iops 2000 --region <your-region> --availability-zone <your-az> --tag-specifications 'ResourceType=volume,Tags=[{Key=Purpose,Value=MyAppData}]'
  1. Set up a lifecycle hook for your Auto Scaling Group to attach the volume when a new instance is launched.

  2. In your user data script or a separate script triggered by the lifecycle hook:

    • Retrieve the volume ID using the tag.
    • Attach the volume using the AWS CLI.
    • Wait for the volume to be in the "attached" state.
    • Format the volume if it's not already formatted.
    • Mount the volume to a directory like /data.
    • Add an entry to /etc/fstab for persistent mounting.
  3. Remember to handle detaching and potential cleanup in a termination lifecycle hook.

This approach provides a more robust way to manage multi-attach volumes with Auto Scaling Groups. However, be aware that using multi-attach volumes requires careful consideration of your application's ability to handle concurrent writes from multiple instances.
Sources
Enable Multi-Attach for an Amazon EBS volume - Amazon EBS
AttachVolume - Amazon Elastic Compute Cloud
Use Amazon EBS Multi-Attach to attach volumes in Amazon EKS | AWS re:Post
Amazon EBS Features - Amazon Web Services

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.