user3398900
user3398900

Reputation: 845

Unable to mount volume created by terraform

I am using the following terraform template

resource "aws_instance" "ec2" {
  ami           = "${var.ami_id}"
  instance_type = "${var.flavor}"
  key_name = "${var.key_name}"
  availability_zone = "${var.availability_zone}"
  security_groups= ["${var.security_group}"]
  tags = {Name = "${var.instance_name}"}
  }

resource "aws_volume_attachment" "ebs_volume" {
  device_name = "/dev/sdg"
  volume_id = "vol-006d716dad719545c"
  instance_id = "${aws_instance.ec2.id}"
}

to launch an instance in aws and attach volume to that instance.

When i execute this i see that the instance is created and volume is attached to the instance as well.

ubuntu@ip-172-31-10-43:~$ lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0   91M  1 loop /snap/core/6350
loop1     7:1    0   18M  1 loop /snap/amazon-ssm-agent/930
loop2     7:2    0 88.4M  1 loop /snap/core/6964
loop3     7:3    0   18M  1 loop /snap/amazon-ssm-agent/1335
xvda    202:0    0   50G  0 disk
└─xvda1 202:1    0   50G  0 part /
xvdg    202:96   0   20G  0 disk

But when i try to mount the volume im getting this weird error

ubuntu@ip-172-31-10-43:~$ sudo mkdir -p /goutham
ubuntu@ip-172-31-10-43:~$ sudo mount /dev/xvdg /goutha,
mount: /goutha,: mount point does not exist.
ubuntu@ip-172-31-10-43:~$ sudo mount /dev/xvdg /goutham
mount: /goutham: wrong fs type, bad option, bad superblock on /dev/xvdg, missing codepage or helper program, or other error.

Can anyone please help me out as to what mistake i am doing in this exercise.

Thanks in advance.

Upvotes: 1

Views: 1774

Answers (2)

RyanKim
RyanKim

Reputation: 1775

You can make a file system on an attached disk using user data and terraform script.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

https://www.terraform.io/docs/providers/aws/r/instance.html#user_data

  1. Create a sh file, templates/mkfs.sh
#!/bin/bash
while ! ls /dev/xvdg > /dev/null
do 
    sleep 5
done

if [ `file -s /dev/xvdg | cut -d ' ' -f 2` = 'data' ]
then
    mkfs.xfs /dev/xvdg
fi
  1. terraform script,
data "template_file" "mkfs" {
  template = "${file("${path.module}/templates/mkfs.sh")}"
}

resource "aws_instance" "ec2" {
  ...
  user_data = "${data.template_file.mkfs}"
  ...
}

It will be run when an ec2 instance is created and wait until disk is mounted. after that it will create file system.

Upvotes: 2

user3398900
user3398900

Reputation: 845

I figured it i think i missed creating the file system in the volume as the volume im trying to attach is an empty volume

so this helped me out

$ sudo mkfs -t xfs /dev/xvdg

and

sudo mkdir -p /goutham

sudo mount /dev/xvdg /goutham

Thanks

Upvotes: 0

Related Questions