resource "aws_instance" "recreate"{
ami = "${var.ami}"
instance_type = "${var.instance_type}"
availability_zone = "${var.availability_zone}"
tags = {
name = "${var.name}"
}
}
data "aws_ebs_volumes" "get_all_volumes"{
filter {
name = "attachment.status"
values = ["attaching", "attached", "detaching"]
}
filter {
name = "size"
values = [8]
}
filter {
name = "attachment.instance-id"
values = ["i-04651fcafa7086fc7"]
}
}
resource "aws_volume_attachment" "ebs_att" {
for_each = data.aws_ebs_volumes.get_all_volumes.ids
device_name = "/dev/" #need to get the index of for_each or something like that
volume_id = each.value
instance_id = "${aws_instance.recreate.id}"
}
In the code above I recreate an ec2 instance that can have different ami, type, etc.
I need to attach the volumes that it had before the recreation and for that I need to interact within a list of device names for my volumes, however I’m already using for_each . I tried to use index() function but it doesen’t work. How can I do it ?