How do you switch a resource from `count` to `for_each` without recreating it?
AdvancedConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer
Change the config, then tell Terraform each old address now maps to a new one using moved blocks (1.1+), so state is rewritten during plan with no API calls.
moved {
from = aws_subnet.private[0]
to = aws_subnet.private["a"]
}
Verify with terraform plan: it should show "has moved" and zero destroys. Without moved blocks you would run terraform state mv for every instance.
More on Loops, Conditionals, Dynamic Blocks and Functions
How does `count` work?BeginnerHow does `for_each` work?BeginnerWhen should you choose `for_each` over `count`?IntermediateHow do you use `for_each` with a list of objects?IntermediateWhat does "The for_each value depends on resource attributes that cannot be determined until apply" mean?AdvancedHow do you create a resource conditionally?Beginner