0

I am deploying a bunch of EC2 instances that require a mount called /data, this is a seperate disk that I am attaching using volume attach in AWS.

Now when I did the following manually it works fine, so the script I use works however when adding it via userdata I am seeing issues and the mkfs command is not happening.

If you see my terraform config:

resource "aws_instance" "riak" {
  count = 5
  ami = "${var.aws_ami}"
  vpc_security_group_ids = ["${aws_security_group.bastion01_sg.id}","${aws_security_group.riak_sg.id}","${aws_security_group.outbound_access_sg.id}"]
  subnet_id = "${element(module.vpc.database_subnets, 0)}"
  instance_type = "m4.xlarge"
  tags {
    Name = "x_riak_${count.index}"
    Role = "riak"
  }
  root_block_device {
    volume_size = 20
  }
  user_data = "${file("datapartition.sh")}"
}

resource "aws_volume_attachment" "riak_data" {
  count = 5
  device_name = "/dev/sdh"
  volume_id  = "${element(aws_ebs_volume.riak_data.*.id, count.index)}"
  instance_id = "${element(aws_instance.riak.*.id, count.index)}"
}

And then the partition script is as follows:

#!/bin/bash

if [ ! -d /data ];
then mkdir /data
fi

/sbin/mkfs -t ext4 /dev/xvdh;

while [ -e /dev/xvdh ] ; do sleep 1 ; done

mount /dev/xvdh /data

echo "/dev/xvdh /data ext4 defaults 0 2" >> /etc/fstab

Now when I do this via terraform the mkfs doesn't appear to happen and I see no obvious errors in the syslog. If I copy the script manually and just bash script.sh the mount is created and works as expected.

Has anyone got any suggestions here?

Edit: It's wort noting adding this in AWS gui under userdata also works fine.

2
  • 1
    Likely /dev/xvdh hasn't been attached yet; why don't you throw a 30 second sleep after that first conditional and give it a try Commented Dec 8, 2017 at 13:51
  • Thanks for this, originally there was a 30 second sleep it must have been lost in an edit. I also thought it was this, adding log lines suggested otherwise (I could see all my log lines but not any to do with the mkfs). Thanks for the response Commented Dec 8, 2017 at 14:09

1 Answer 1

3

You could try with remote_exec instead of user_data.

User_data relates on cloud-init which can act differently depending on images of your cloud provider.

And also i'm not sure it's a good idea to exec a script that would wait for some time before executing in the cloud-init section => this may lead to VM considering launch has failed because of a timeout (depending on your cloud provider).

Remote_exec may be better here because you will be able to wait until your /dev/xvdh is attached

See here

resource "aws_instance" "web" {
  # ...

  provisioner "file" {
    source      = "script.sh"
    destination = "/tmp/script.sh"
  }

  provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/script.sh",
      "/tmp/script.sh args",
    ]
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

And also here : github.com/hashicorp/terraform/issues/4668 => some hacks regarding how to wait for full boot before launching a script
I have a feeling that the volume attachment is happening after this script is being run. This caused timeouts with the volumes being created.
I have moved it to the aws_volume_attachment block and re-testing
Same issue :( ` aws_volume_attachment.riak_data.0: Still creating... (3m40s elapsed) aws_volume_attachment.riak_data.1: Still creating... (3m50s elapsed) aws_volume_attachment.riak_data.3: Still creating... (3m50s elapsed) aws_volume_attachment.riak_data.2: Still creating... (3m50s elapsed) aws_volume_attachment.riak_data.4: Still creating... (3m50s elapsed) `
I certainly can, I believe this may be an issue with that fact I use a bastion host though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.