Menu

1 / 2

Terraform moved blocks: refactor without destroying anything

DEV Community: terraform·Abdelrhman Fikri·3 days ago
#Lebt3vZS
#devtoterraform#devto#dev#local_file#worker
Long article (774 words) — no AI summary yet.
Reading 0:00
15s threshold

I wrapped one resource in a module and switched another from count to for_each . Then I ran terraform plan . It wanted to destroy all three resources and create three new ones.

Same files, different addresses. Terraform can't tell they're the same objects.

On a local file that's nothing. On a database it's a bad afternoon. So I kept the plan with and without moved blocks. This was Terraform 1.17.2 with the local provider, local state, no cloud account.

Starting point

One config file at the root, plus two workers made with count :

resource "local_file" "app_config" { filename = "${path.module}/out/app.conf" content = "port=8080\n" }

resource "local_file" "worker" { count = 2 filename = "${path.module}/out/worker-${count.index}.conf" content = "worker=${count.index}\n" }

Enter fullscreen mode

Exit fullscreen mode

terraform apply ended with:

Apply complete!…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.