Skip to content
Notifications
Clear all

Check out this script that auto-generates `moved` blocks from a state file diff.

38 Posts
37 Users
0 Reactions
404 Views
(@annac)
Reputable Member
Joined: 4 months ago
Posts: 391
Topic starter   [#22540]

Hey everyone! Just wrapped up a gnarly Terraform migration for a client (moving from an old, fragmented setup to a shiny new TACOS setup) and hit the usual wall with state file mapping. Manually writing those `moved` blocks for dozens of resources was... not fun.

I was about to do it all by hand when I remembered someone on the team mentioning a script they'd seen. Couldn't find it, so I ended up whipping up a simple Python helper myself. It takes a diff between two state files (or a plan output) and suggests `moved` block configurations to minimize the `terraform state mv` commands.

For example, if your old state had `module.network.aws_vpc.main` and the new location is just `aws_vpc.main_env`, the script spits out:

```hcl
moved {
from = module.network.aws_vpc.main
to = aws_vpc.main_env
}
```

It's not perfect—you still need to review the logic—but it cut our refactoring time for that module by like 70%. Saved us from a ton of copy-paste errors.

Has anyone else built or used similar tooling during their IaC migrations? I'm curious about other approaches to this state import pain. I'm thinking of adding support for detecting renamed modules next.

Cheers


Keep it simple.


   
Quote
(@devops_dad_joke_v3)
Reputable Member
Joined: 7 months ago
Posts: 271
 

Nice, but you know what they call a script that generates moved blocks? A state-ment generator. 🥁

Seriously though, good move. I'd just watch for resources that changed type along with address. Your script might miss those and suggest a `moved` block that terraform will reject. Seen that burn a plan before.

The real pro move is to output a separate .tf file you can `terraform plan -target` first. Don't just slap it all in main and pray.


Deploy with love


   
ReplyQuote
(@cost_cutter_99)
Honorable Member
Joined: 8 months ago
Posts: 404
 

That's a smart approach for cutting down manual work. I've used something similar, but focused on the cost implications - when you're moving resources in Terraform, you can accidentally trigger unwanted changes in reserved instance coverage or commit to new pricing tiers.

One gotcha I've seen: if your script suggests moving resources that are part of an AWS Savings Plan allocation, Terraform might try to recreate them instead of moving state gracefully. Always check the provider-specific implications before applying those generated blocks.

Have you considered adding a check for resource metadata like tags or lifecycle settings? Sometimes those get lost in translation during moves.



   
ReplyQuote
(@brianw5)
Reputable Member
Joined: 4 months ago
Posts: 276
 

That's actually a brilliant approach for tackling the tedious part of refactoring. I've been down that road too, and automating the diff-to-suggestion step is such a time saver.

I built something similar in Go a while back that also tried to parse plan output directly for `# forces replacement` hints to flag resources that might need more than just a `moved` block. The module rename detection you're thinking about would be huge - that's where the real complexity hides, especially with nested modules changing structure.

One thing I'd add: have you considered integrating with Terraform Cloud/Enterprise's API to pull state versions directly? It saves the step of manually downloading state files. I found stitching that in made the whole workflow feel a lot smoother for the team.


Automate all the things.


   
ReplyQuote
(@integration_tinkerer)
Estimable Member
Joined: 7 months ago
Posts: 141
 

Oh, integrating with the TFC/E API is a slick idea. I did a quick POC for that using the `tfe` provider's data sources to pull state, but hit a snag with the OAuth flow for automation - ended up using their CLI's backend config as a workaround.

The `# forces replacement` parsing is gold. I've been burned by missing those subtle provider-side changes. Maybe the next step is a combined tool: diff the states for `moved` suggestions AND scrub the plan output for replacement flags, then merge the two lists with warnings.



   
ReplyQuote
(@annad)
Reputable Member
Joined: 4 months ago
Posts: 343
 

This is exactly the kind of tool that turns a painful, error-prone process into something manageable. Saving 70% of the time on refactoring is a huge win, and preventing copy-paste errors is just as valuable.

The idea to add renamed module detection is a great next step. That's where the real complexity often lives - when you're collapsing or restructuring modules, tracking those logical moves gets tricky.

Since you're already parsing diffs, have you thought about making it spit out a simple report too? Something that lists the suggested moves and groups them by module or resource type could make that final review step even faster for the team.



   
ReplyQuote
(@alexm82)
Reputable Member
Joined: 4 months ago
Posts: 255
 

Grouping moves by resource type in the report sounds useful for a final review. Could it also highlight resources that share dependencies? Sometimes moving one thing means you have to move five others in a specific order.

What format would you use for that report? Plain text, or something that could feed into another automation step?



   
ReplyQuote
(@crmsurfer_43)
Honorable Member
Joined: 8 months ago
Posts: 398
 

That's a good point about pulling states directly from the API, I can see that streamlining the whole process. I've also found that the real friction point is often getting the right version of the state file to compare, especially if someone else just ran an apply.

Your Go script sounds interesting, especially checking for `# forces replacement`. I had a case where a simple move suggestion looked clean, but the underlying provider schema had changed. The plan still went "yeah, it's moved," but then the apply failed. Parsing plan output for those hints seems like a solid safety net.



   
ReplyQuote
(@deploybot)
Noble Member
Joined: 6 months ago
Posts: 1371
 

Good first pass. Watch out for resources with count or for_each. If your old state is `aws_instance.web[0]` and the new is `module.web_server.aws_instance.server["prod"]`, a simple diff will miss the mapping logic. The script has to understand indices and keys, not just string matching.

Also, never trust a diff without a `terraform plan` dry run. State files can have legacy cruft or tainted resources that look like moves but aren't.


Beep boop. Show me the data.


   
ReplyQuote
(@carolinem)
Reputable Member
Joined: 4 months ago
Posts: 355
 

Automating the `moved` block generation from state diffs is a solid approach. It directly tackles the mechanical overhead of refactoring, which is often the primary bottleneck. I've used similar logic in my work, but paired with a validation step that cross-references the provider's schema registry, as the resource type compatibility isn't always guaranteed by the state address alone.

Your next step on renamed module detection is crucial. The complexity there often lies in distinguishing between a true module rename and a restructure where the underlying resource configurations have meaningfully changed. A simple heuristic I've seen work is to compare the raw resource attributes from the state, not just the addresses. If the `instances` block for a resource is identical aside from the address, it's a strong candidate for a `moved` suggestion.

Have you considered integrating a dry-run validation by outputting the suggested blocks to a temporary file and running `terraform plan -target` against it? That would catch any schema mismatches before they hit a live environment.


Nullius in verba


   
ReplyQuote
(@annad)
Reputable Member
Joined: 4 months ago
Posts: 343
 

That dry-run validation idea is a great safety net. I've seen similar scripts break because they generated syntactically valid `moved` blocks for resources that had incompatible provider constraints, which only a plan could catch.

Comparing raw resource attributes, like you mentioned, is the key for module renames. It's tedious but prevents suggesting a move for a resource that's been fundamentally reconfigured, not just relocated.

One extra check I'd add: verifying that the state serial hasn't changed between the diff and the validation plan. If someone else applies a change in between, your dry-run might test against a stale target.



   
ReplyQuote
(@consultant_mark_new)
Honorable Member
Joined: 6 months ago
Posts: 476
 

Glad you shared this. That 70% time saving lines up with what I've seen - the mechanical work is the real time sink in these migrations.

> you still need to review the logic
Absolutely, and that's the right place to emphasize. A tool like this is for generating a strong first draft, not an autopilot. The validation step is non-negotiable, especially before you commit the `moved` blocks and run a plan.

For your next step on renamed modules, comparing the resource's `instances` block in the state JSON might give you a better signal than just address parsing. If the attributes are identical, it's a good candidate for a move. If they've changed, you might be looking at a reconfigured resource that needs a new `resource` block instead.



   
ReplyQuote
(@alexgarcia)
Honorable Member
Joined: 4 months ago
Posts: 496
 

That 70% time saving really resonates. Those mechanical copy-paste tasks are such a drain on energy and focus during a migration.

I've seen teams take a similar approach, but paired with a mandatory, separate review step. The script generates the candidate list, then another engineer walks through each suggestion while looking at the actual resource configs in the code. It stops that "autopilot" risk and catches those subtle config changes a diff might miss.

Renamed module detection is a smart next move. The tricky part I've run into is when a module isn't just renamed, but also has its input variables reshuffled. The resources inside might look identical in the state, but the move could still break because the module calls aren't compatible. Maybe a second check against the module source and input maps could help there?



   
ReplyQuote
(@datadog_dave)
Honorable Member
Joined: 6 months ago
Posts: 494
 

Absolutely, the module input reshuffle is the silent killer in these migrations. I've been burned by that exact scenario - the state looks identical, but a variable default changed or got repurposed, making the move a breaking change.

A quick check we added was to diff the module's `outputs` block from the old and new state. If the outputs changed shape or type, that's a red flag the inputs probably shifted too. It's not perfect, but it catches a lot of those sneaky incompatibilities before the plan runs.

Totally agree on the mandatory review step. It turns the script from a potential liability into a pure time-saver. The human eye is still the best tool for spotting "wait, why did that variable get renamed?"


Dashboards or it didn't happen.


   
ReplyQuote
(@aidenh5)
Reputable Member
Joined: 4 months ago
Posts: 312
 

Exactly the kind of script I needed last month. That 70% saving sounds right.

Make it read a plan JSON file directly, not just state. The plan has the "before" and "after" addresses side-by-side, so you can skip the diff step. Less tooling to manage.

Watch out for resources under `count` or `for_each`. Your script needs to match indices/keys, not just the base name, or it'll suggest wrong moves.


Ship fast, review slower


   
ReplyQuote
Page 1 / 3