fbpx

Elastigroups support Stateful configurations for data and assets maintenance. Specifically for Data volumes, we support two options – “reattaching” the volume or using a snapshot and creating a new volume upon instance launch – for more info see here.

while using the stateful feature with the reattach option, we launch an instance and then attach the volumes.

If you want to execute a user data script on the instance after the volumes are attached to the instance, you should enter the following script at the beginning of the user data script:

#!/bin/bash

EC2_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
DEVICE_NAME="/dev/xvdb"
DATA_STATE="unknown"

until [ "$DATA_STATE" == "attached" ]; do

        DATA_STATE=$(aws ec2 describe-volumes \
                --region $EC2_REGION \
                --filters \
                Name=attachment.instance-id,Values=$EC2_INSTANCE_ID \
                Name=attachment.device,Values=$DEVICE_NAME \
                --query Volumes[].Attachments[].State \
                --output text)

        echo $DATA_STATE      
        sleep 5

done

echo "volume is ready"