diff --git a/template.tf b/template.tf index aa00c9b..2a6c1d2 100644 --- a/template.tf +++ b/template.tf @@ -3,6 +3,7 @@ data "template_file" "userdata" { template = file("${path.module}/templates/userdata.tpl") vars = { + region = data.aws_region.current.name data_root = "/data" volume = aws_ebs_volume.ebs.id } diff --git a/templates/userdata.tpl b/templates/userdata.tpl index 77b8072..2cbb44a 100644 --- a/templates/userdata.tpl +++ b/templates/userdata.tpl @@ -25,6 +25,9 @@ ACC_ID=$(echo $METADATA | jq -r .accountId) device="xvdf" volume="${volume}" +while [[ $(aws ec2 detach-volume --volume-id=$volume --region=${region}) == *"detaching"* ]]; do sleep 1; done; +while [[ $(aws ec2 attach-volume --volume-id=$volume --instance-id=$(cat /etc/amazon-ec2/instance-id) --device=$device --region=${region}) == *"attaching"* ]]; do sleep 1; done; + # Detect if volume is attached while [[ ! $(/bin/lsblk) == *$device* ]]; do # Detect if we are on a Nitro based VM and then lookup nvme device