Hello,
I have a code which works fine on all cases except when new EBS disk is added to the list. On second apply it destroys all EBS attachments and creates them again.
I also build string for each EBS block with needed values.
resource "aws_ebs_volume" "default" {
for_each = {
# key is name_tag, value is list of strings
for s in local.ebs_block_devices: format("%s-%s",element(split(local.delim, s),local.name_tag_index),element(split(local.delim, s),local.device_name)) => split(local.delim, s)}
availability_zone = "${element(each.value,local.az_index)}"
size = "${element(each.value,local.volume_size_index)}"
type = "${element(each.value,local.volume_type_index)}"
tags = "${merge(
map(
"Name", "${format("%s-%s",local.stackAndClusterName,format("%s-%s",element(each.value,local.name_tag_index),element(each.value,local.device_name)))}"),
"${var.clusterCommon.common_tags}"
)}"
}
resource "aws_volume_attachment" "default" {
for_each = {
# key is name_tag, value is list of strings
for s in local.ebs_block_devices: format("%s-%s",element(split(local.delim, s),local.name_tag_index),element(split(local.delim, s),local.device_name)) => split(local.delim, s)
}
device_name = "${element(each.value,local.device_name)}"
volume_id = "${lookup(aws_ebs_volume.default, each.key).id}"
instance_id = "${lookup(aws_instance.ec2, element(split("-/", each.key), local.name_tag_index)).id}"
}
PLAN shows this:
# module.EC2.aws_volume_attachment.default["B1./dev/sdf"] must be replaced
-/+ resource "aws_volume_attachment" "default" {
device_name = "/dev/sdf"
~ id = "vai-3606475186" -> (known after apply)
instance_id = "i-09a547764c468f3fb"
~ volume_id = "vol-0c9a0a822870ab11d" -> (known after apply) # forces replacement
}
Why this is happening?