3
votes

I am creating an ec2-instance with the custom AMI that has 2 volumes. I want to give separate names/tags to both of the volumes like

vol-lab-cam-web-aue1-sda1 to the root volume and vol-lab-cam-web-aue1-sdb to the other volume using terraform.

I can only see a volume_tags option in terraform docs. Is there any workaround for doing this ?? My sample code is below

resource "aws_instance" "web" {
ami           = "ami-0e6f18d5546ceec3d"
instance_type = "t2.micro"
volume_tags = {
  Name = "vol-lab-cam-web01-aue1a-sda1"
  }

tags = {
  Name = "HelloWorld"
  }
}

Following is the Screenshot of what i get using this sdb

sda1

2
Is there a reason you need to tag the volumes with the device ID? - ydaetskcoR
Yes because these AMIs have multiple volumes and there are more than 100 ec2 instances. Its hard to find which one is root or not. - Sohaib Mustafa
Why do you need to identify the root/non root volumes from outside the instance? - ydaetskcoR
Because at times we need to increase volume size that are non root, we dont want to go in the situation by stopping or detaching root volume from the servers. - Sohaib Mustafa
Did you ever find a solution for this? - D Swartz

2 Answers

1
votes

Since volume_tags gets applied uniformly to all devices created at launch time, don't use it for device-specific values. Instead, use the aws_ebs_volume resource to create the sdb volume and the aws_volume_attachment resource to attach it to the instance.

In the aws_ebs_volume code, you can enter "vol-lab-cam-web-aue1-sdb" for the Name value.

1
votes

Like KLH says if you create 2 volumes in Terraform:

resource "aws_ebs_volume" "drivea" {
  availability_zone = "${var.availability_zone}"
  size              = 40

  tags = {
    Name = "HelloWorld"
  }
}

resource "aws_ebs_volume" "driveb" {
  availability_zone = "${var.availability_zone}"
  size              = 40

  tags = {
    Name = "WhaleTail"
  }
}

Like thus and then attach them to your instance:

resource "aws_volume_attachment" "ebsa" {
  device_name = "/dev/sdh"
  volume_id   = "${aws_ebs_volume.drivea.id}"
  instance_id = "${aws_instance.web.id}"
}

resource "aws_volume_attachment" "ebsb" {
  device_name = "/dev/sdi"
  volume_id   = "${aws_ebs_volume.driveb.id}"
  instance_id = "${aws_instance.web.id}"
}

You will have 2 disks attached to your instance with different tags. Then if you look in your AWS console you will see the different volumes with different tags.