How to attach multiple ebs volumes to each ec2 instance

I have this code. Basically, I am creating 2 new ec2 instances, and 4 ebs volumes. I want to attach 2 ebs volumes to each ec2 instance.
device names I chose are ["/dev/sde", “/dev/sdf”] …

if everything works i should see
on first ec2:
/dev/sde
/dev/sdf

on second ec2:
/dev/sde
/dev/sdf

I tried with below code, but 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"

this is the code I am applying


resource "aws_ebs_volume" "test_volume" {
  count       = var.ec2_count * var.test_ebs_volume_count
  size= 16000
  type = "gp2"
  availability_zone = "us-east-1b"
  tags = {
         Name = "1e-volume-${count.index + 1}"
  }

}


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
}

how can I correct this code to have 2 devices on each ec2 instance with ebs volumes attached?

Hi @ugenderr

In different linux flavors and releases the name of the device names follows a standard, unfortunately you can’t decide what names you want. The disk technology is also important.

See:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html

To debug, create the instances and the volumes with Terraform and use the EC2 web console to attach and learn the names, what works or not… Then do the same in Terraform.

See this example, no “/dev/sde” but /dev/nvme0n1p1 (nvme is the EBS disk technology)

$ lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0          7:0    0   22M  1 loop /snap/amazon-ssm-agent/4047
...
nvme0n1      259:0    0    8G  0 disk 
├─nvme0n1p1  259:1    0  7.9G  0 part /
└─nvme0n1p15 259:2    0   99M  0 part /boot/efi