give different volume tags while creating ec2 instance from custom AMI

Viewed 1974

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 Answers

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.

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.

Related