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?