Terraform 0.12.6
Code snippet
data "aws_ec2_transit_gateway_vpn_attachment" "vpn_propagations" {
for_each = { for v in var.vpn_propagations : v => v }
transit_gateway_id = var.transit_gateway_id
vpn_connection_id = v.value
}
resource "aws_ec2_transit_gateway_route_table_propagation" "vpn_propagations" {
for_each = { for v in var.vpn_propagations : v => v }
transit_gateway_attachment_id = data.aws_ec2_transit_gateway_vpn_attachment.vpn_propagations[v.key].id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.route_table.id
}
Terraform plan - after an item is removed from vpn_propagations[ ]
Not sure why terraform is trying to replace vpn_propagations[2]. Shouldn’t it destroy vpn_propagations[2] and move vpn_propagations[3] to [2] in state file?
(Or am I using for_each
the wrong way?)
1 Like
Hi @arunamat!
It seems that something strange is going on here, because a resource using for_each
should never have numeric instance keys like [0]
, [1]
, etc. Instead, it should have string keys like ["foo"]
where foo
is a key from your map.
Is it possible that the resources being planned here are from a different module than the one whose code you shared here? Perhaps it’s an older version of the module that’s still using count
? 
Hi @apparentlymart - They are from the same module.
State file snippet -
I believe the above state format is for for_each
^^
When for_each
is used, the "index_key"
in the state appears as a string, so it still seems that something strange is happening here.
Is this the first time running terraform apply
or terraform plan
after switching from count
to for_each
?
No @apparentlymart.
Also I did do a terraform destroy
and terraform apply
with for_each
and TF 0.12.16
- just to make sure that it was not something to do with the state migration.
1 Like
Hi again @arunamat!
I’m still not sure what exactly is going on here. Would you mind opening a bug report about this behavior? The various bits of information requested in that issue template should hopefully give the engineers on the Terraform team more information to understand better what exactly is going on here and fix it.
If you do open one, please post a link to it here for future readers of this thread to see, and also link to this thread from the issue so that the engineering team can see the context from the discussion we already had here.
Thanks!
Thanks @apparentlymart. After some debugging we figured out that our root module was pointing to the wrong code base using count
. After switching to the right module version everything is working as expected. I’m really sorry for reporting a false issue.
Hello @arunamat, currently i’m facing the same issue with terraform 1.2.0 but i don’t know what exactly happening here, could you please guide me what changes you have done to work this.
cheers,
@arunamat i guess i have resolved the issue myself, thanks