0
votes

I am trying to attach multiple ebs volumes to each instance. In this particular scenario, I am trying to attach two ebs volumes (/dev/sde, /dev/sdf) to each of the two ec2 instances I am creating. The number of ebs volumes and the number of ec2 instances are variables (meaning they can change. 3 ebs volumes to each ec2 for total of 3 ec2s etc so they are not constant).

But some reason, I am getting this error

Error: Error attaching volume (vol-0f1ace71d7af68b36) to instance
(i-029671a0d4d152761), message: "Invalid value '/dev/sde' for unixDevice. Attachment 
point/dev/sde is already in use", code: "InvalidParameterValue"

so here is the block for that attachment work

resource "aws_volume_attachment" "volume_attachement" {
  count       = var.ec2_count * var.test_ebs_volume_count
  volume_id   = aws_ebs_volume.test_volume.*.id[count.index]
  device_name = element(var.ec2_device_names, (count.index))
  instance_id = element(aws_instance.data_node.*.id, ((count.index+1)%2))
}


variable "ec2_device_names" {
  description = "multiple devices for each ec2 instance"
  default = [
    "/dev/sde",
    "/dev/sdf"
  ]
}


variable "ec2_count" {
  default = 2
}

variable "test_ebs_volume_count" {
  default = 2
}

variable "test_ebs_volume_size" {
 default = 16000
}

I am able to see all 4 ebs volumes being created, and 2 ec2 instances being created. But one ec2 instance has /dev/sde attached, and the other one is attached to on the other ec2 to /dev/sdf

I wanted to see both /dev/sde, and /dev/sdf attached to first ec2 instance and second ec2 instance.

  first ec2:
                /dev/sde    
                /dev/sdf     

  second ec2:
               /dev/sde
               /dev/sdf

I know there is something I am missing in "aws_volume_attachment" block code. Any suggestions would be greatly appreciated.