<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdelrhman Fikri</title>
    <description>The latest articles on DEV Community by Abdelrhman Fikri (@abdelrhman_fikri).</description>
    <link>https://dev.to/abdelrhman_fikri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4278863%2F2b05bd7c-dc4f-4b0f-b2c9-894bd7b6572a.png</url>
      <title>DEV Community: Abdelrhman Fikri</title>
      <link>https://dev.to/abdelrhman_fikri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdelrhman_fikri"/>
    <language>en</language>
    <item>
      <title>Terraform moved blocks: refactor without destroying anything</title>
      <dc:creator>Abdelrhman Fikri</dc:creator>
      <pubDate>Wed, 02 Dec 2026 16:11:05 +0000</pubDate>
      <link>https://dev.to/abdelrhman_fikri/terraform-moved-blocks-refactor-without-destroying-anything-2pm3</link>
      <guid>https://dev.to/abdelrhman_fikri/terraform-moved-blocks-refactor-without-destroying-anything-2pm3</guid>
      <description>&lt;p&gt;I wrapped one resource in a module and switched another from &lt;code&gt;count&lt;/code&gt; to &lt;code&gt;for_each&lt;/code&gt;. Then I ran &lt;code&gt;terraform plan&lt;/code&gt;. It wanted to destroy all three resources and create three new ones.&lt;/p&gt;

&lt;p&gt;Same files, different addresses. Terraform can't tell they're the same objects.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Starting point
&lt;/h2&gt;

&lt;p&gt;One config file at the root, plus two workers made with &lt;code&gt;count&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"local_file"&lt;/span&gt; &lt;span class="s2"&gt;"app_config"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"${path.module}/out/app.conf"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"port=8080\n"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"local_file"&lt;/span&gt; &lt;span class="s2"&gt;"worker"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;count&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"${path.module}/out/worker-${count.index}.conf"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"worker=${count.index}\n"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;terraform apply&lt;/code&gt; ended with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The refactor
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;app_config&lt;/code&gt; goes into &lt;code&gt;modules/app/&lt;/code&gt; and gets called as &lt;code&gt;module "app"&lt;/code&gt;. &lt;code&gt;worker&lt;/code&gt; switches to &lt;code&gt;for_each&lt;/code&gt;, keyed by name instead of index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"app"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./modules/app"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;locals&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;workers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;primary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;replica&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"local_file"&lt;/span&gt; &lt;span class="s2"&gt;"worker"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;for_each&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;workers&lt;/span&gt;
  &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"${path.module}/out/worker-${each.value}.conf"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"worker=${each.value}\n"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One detail inside the module: I wrote the filename as &lt;code&gt;${path.root}/out/app.conf&lt;/code&gt;, not &lt;code&gt;path.module&lt;/code&gt;. That keeps it at &lt;code&gt;./out/app.conf&lt;/code&gt;. With &lt;code&gt;path.module&lt;/code&gt; the path would move into the module folder, and a changed &lt;code&gt;filename&lt;/code&gt; means a new file. I didn't try that variant. If a plan still shows a replace after you've added your move blocks, check paths like this first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plan without moved blocks
&lt;/h2&gt;

&lt;p&gt;I ran &lt;code&gt;terraform plan&lt;/code&gt; against the existing state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # local_file.app_config will be destroyed
  # (because local_file.app_config is not in configuration)
  # local_file.worker[0] will be destroyed
  # (because resource does not use count)
  # local_file.worker[1] will be destroyed
  # (because resource does not use count)
  # local_file.worker["primary"] will be created
  # local_file.worker["replica"] will be created
  # module.app.local_file.app_config will be created

Plan: 3 to add, 0 to change, 3 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(I cut the attribute detail. The headline lines are all there.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Plan with moved blocks
&lt;/h2&gt;

&lt;p&gt;I added a &lt;code&gt;moved.tf&lt;/code&gt;, one block per instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;moved&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app_config&lt;/span&gt;
  &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app_config&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;moved&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"primary"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;moved&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;local_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"replica"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the plan reads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # local_file.worker[0] has moved to local_file.worker["primary"]
  # local_file.worker[1] has moved to local_file.worker["replica"]
  # local_file.app_config has moved to module.app.local_file.app_config

Plan: 0 to add, 0 to change, 0 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I applied it. &lt;code&gt;0 added, 0 changed, 0 destroyed&lt;/code&gt;, which looks like nothing happened. But that apply is the step that rewrites the addresses in state. A second plan said &lt;code&gt;No changes&lt;/code&gt; and exited 0 with &lt;code&gt;-detailed-exitcode&lt;/code&gt;. &lt;code&gt;terraform state list&lt;/code&gt; showed the new addresses. The IDs hadn't changed (worker 0 was &lt;code&gt;1e99b877...&lt;/code&gt; before and after), and all three files were still in &lt;code&gt;out/&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I'd check again
&lt;/h2&gt;

&lt;p&gt;Read the plan for "has moved". &lt;code&gt;0 to destroy&lt;/code&gt; is the line that matters, not the absence of red.&lt;/p&gt;

&lt;p&gt;Match the old key exactly. &lt;code&gt;count&lt;/code&gt; gives you numbers like &lt;code&gt;worker[0]&lt;/code&gt;, &lt;code&gt;for_each&lt;/code&gt; wants strings like &lt;code&gt;worker["primary"]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Keep the refactor commit to addresses only. Change &lt;code&gt;content&lt;/code&gt; in the same commit and you get a normal in-place change mixed in with the move, which is hard to review.&lt;/p&gt;

&lt;p&gt;Leave the &lt;code&gt;moved&lt;/code&gt; blocks in while anyone might still apply an older state, like other teams using your module. Check the Terraform docs for your version before you delete them.&lt;/p&gt;

&lt;p&gt;For shared code I'd rather use &lt;code&gt;moved&lt;/code&gt; than &lt;code&gt;terraform state mv&lt;/code&gt;. It goes through a pull request and replays in every environment, so nobody has to remember a manual step.&lt;/p&gt;

&lt;p&gt;That was all local files. I haven't tried azurerm or aws, remote state, or locking, so read your own plan before you apply.&lt;/p&gt;

&lt;p&gt;The full write-up with the script that produced every output above is &lt;a href="https://runbook-lab.pages.dev/posts/terraform-moved-block-module-count-to-for-each/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. I also put a few Terraform modules I've tested offline (an S3 bucket and an Azure storage account) in a &lt;a href="https://fikriwave7.gumroad.com/l/nacamf" rel="noopener noreferrer"&gt;free pack&lt;/a&gt;. I made it. No real AWS or Azure account has seen those modules yet.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: I wrote this with an AI assistant. The commands were run for real in a scratch folder and the outputs above are copied from that run.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>devops</category>
      <category>iac</category>
      <category>refactoring</category>
    </item>
  </channel>
</rss>
