Auto-mount EBS using terraform

Attaching an EBS volume using terraform is straight forward but when instance is started , you find out that your attached volume is not shown even though its attached. Terraform code from my main.tf file  to attach an EBS volume along with the root volume during instance creation is shown below.


resource "aws_instance" "server" {
  instance_type = "${var.instance_type}"
  ami = "${var.ami_id}"

tags {
  Name = "${var.project_name}_Jenkins_master"
}

key_name = "${var.key_name}"
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
subnet_id = "${var.subnets}"
user_data = "${data.template_file.user-init.rendered}"

root_block_device {
  volume_size = "40"
  volume_type = "standard"
}

ebs_block_device {
  device_name = "/dev/sdb"
  volume_size = "150"
  volume_type = "standard"
}
}

Now as soon you run terraform  with this code where you are attaching two volumes one as root which is  40 GB in size and another   one  which is 150 GB. Now if you login to the instance created after running this  code you will notice something like this.

mount

Lets solve this problem and  automount this disk as soon instance is created.

We will solve this problem by supplying the userdata  to the terraform script  and we will mention the steps to automount the  added partition during instance creation.

create a terraform template file[although its not required for this example as we are not using any variables in template but still it also act as an example for using template files in terraform.] in the same directory where your main.tf file is located.Lets name it userdata.tpl . Below is the content of my file.

sudo file -s /dev/xvdb |grep -w "/dev/xvdb: data"
exit_status=`echo $?`
if [[ $exit_status == 0 ]];then
    sudo mkfs -t ext4 /dev/xvdb
    sudo mkdir -p /jenkins_home
    sudo mount /dev/xvdb /jenkins_home
else
    sudo mkdir -p /jenkins_home
    sudo mount /dev/xvdb /jenkins_home
fi

Now modify your main.tf file and add necessary code to render this template at run time.

data "template_file" "user-init" {
  count    = 1
  template = "${file("${path.module}/userdata.tpl")}"<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
}

Full main.tf file looks as shown below

data "template_file" "user-init" {
  count    = 1
  template = "${file("${path.module}/userdata.tpl")}"
}

resource "aws_instance" "server" {
  instance_type = "${var.instance_type}"
  ami           = "${var.ami_id}"

  tags {
    Name = "${var.project_name}_Jenkins_master"
  }

  key_name               = "${var.key_name}"
  vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
  subnet_id              = "${var.subnets}"
  user_data              = "${data.template_file.user-init.rendered}"

  root_block_device {
    volume_size = "40"
    volume_type = "standard"
  }

  ebs_block_device {
    device_name = "/dev/sdb"
    volume_size = "150"
    volume_type = "standard"
  }
}

Now run your terraform code and notice that attached EBS volume is auto-mounted. To make things permanent so that it also auto-mounts on instance reboot, also make an entry to /etc/fstab file.

If you notice some issues check /var/log/cloud-init-output.log as all the user-data bogs are redirected to the same.

All variables in above code blocks are defined separately in variables.tf file.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s