Reputation: 465
I would like to attach an EBS volume not a snapshot as a persistent store for my spot instances. I understand how to manually attach the volume, mount it and get it to survive reboots but how would I get it to automatically get attached at startup?
Is there something I could do in the user data at launching the instance?
Presently I have a ami that I run as a spot instance. I have a separate volume that persists and is used for both input to the instance and to save results. I only ever have one instance up at a time. The ami mounts the drive at /data. For the mount to survive reboots, I have edited /etc/fstab
to include:
UUID=MY_VOLUME_UUID /data xfs defaults,nofail 0 2
Again Edited to show Passatizhi's Solution
I added the following to the Configure Instance Details > Advanced Details > User data part of the EC2 launch wizard:
#!/bin/bash
INSTANCE_ID=$(curl 169.254.169.254/latest/meta-data/instance-id)
export AWS_DEFAULT_REGION=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//')
/home/ubuntu/miniconda3/bin/aws ec2 attach-volume --volume-id vol-myVol12345 --instance-id $INSTANCE_ID --device /dev/sdf
sleep 10
sudo mkdir -p /data
sudo mount /dev/nvme1n1 /data
Note:
I needed to add the full path to aws to get it to work. Also as the ami already has the /data setup I don't need the sudo mkdir -p /data
Upvotes: 1
Views: 944
Reputation: 138
#!/bin/bash INSTANCE_ID=$(curl 169.254.169.254/latest/meta-data/instance-id)
export AWS_DEFAULT_REGION=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//')
/bin/aws ec2 attach-volume --volume-id vol-0fdb738415896f8f6 --instance-id $INSTANCE_ID --device /dev/sdf
sleep 10
sudo mkdir -p /data
sudo mount /dev/nvme1n1 /data
Try /bin/aws instead aws. I used t3.small, so /dev/nvme1n1
Upvotes: 2