<?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: DevOps in Practice</title>
    <description>The latest articles on DEV Community by DevOps in Practice (@devops_in_practice).</description>
    <link>https://dev.to/devops_in_practice</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%2F4280213%2Ff2dd2d77-41dc-4c1d-91b1-9efee0b43a45.png</url>
      <title>DEV Community: DevOps in Practice</title>
      <link>https://dev.to/devops_in_practice</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devops_in_practice"/>
    <language>en</language>
    <item>
      <title>Terraform's sensitive = true doesn't protect your secrets. Here's what does</title>
      <dc:creator>DevOps in Practice</dc:creator>
      <pubDate>Thu, 03 Dec 2026 10:34:00 +0000</pubDate>
      <link>https://dev.to/devops_in_practice/terraforms-sensitive-true-doesnt-protect-your-secrets-heres-what-does-3k08</link>
      <guid>https://dev.to/devops_in_practice/terraforms-sensitive-true-doesnt-protect-your-secrets-heres-what-does-3k08</guid>
      <description>&lt;p&gt;Marking a Terraform output &lt;code&gt;sensitive = true&lt;/code&gt; feels like protection. It isn't, at least not where it matters. Here's a two-minute proof, and what to do instead. Everything below is from a real run on Terraform 1.17.&lt;/p&gt;

&lt;h2&gt;
  
  
  The proof
&lt;/h2&gt;

&lt;p&gt;A random database password, returned by an output marked sensitive:&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;"random_password"&lt;/span&gt; &lt;span class="s2"&gt;"db"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"db_password"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;value&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random_password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;
  &lt;span class="n"&gt;sensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After &lt;code&gt;terraform apply&lt;/code&gt;, the CLI hides it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ terraform output
db_password &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;sensitive&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now read the state file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ jq -r &lt;span class="s1"&gt;'.resources[0].instances[0].attributes.result'&lt;/span&gt; terraform.tfstate
fqK5VEtF+v:imR-u
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's the password, in plain text.&lt;/p&gt;

&lt;h2&gt;
  
  
  What sensitive actually does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sensitive = true&lt;/code&gt; is a &lt;strong&gt;display&lt;/strong&gt; setting. It:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;redacts the value in &lt;code&gt;terraform plan&lt;/code&gt; and &lt;code&gt;terraform apply&lt;/code&gt; output&lt;/li&gt;
&lt;li&gt;shows &lt;code&gt;&amp;lt;sensitive&amp;gt;&lt;/code&gt; in &lt;code&gt;terraform output&lt;/code&gt; (though &lt;code&gt;terraform output -raw db_password&lt;/code&gt; prints it)&lt;/li&gt;
&lt;li&gt;spreads to everything derived from that value, so it doesn't leak through another output by accident&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What it does &lt;strong&gt;not&lt;/strong&gt; do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encrypt anything&lt;/li&gt;
&lt;li&gt;keep the value out of the &lt;strong&gt;state file&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;keep it out of a &lt;strong&gt;saved plan file&lt;/strong&gt; (&lt;code&gt;terraform plan -out=tfplan&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Terraform needs the real value to compare it on the next plan, so for a normal resource it stores it. That's by design.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to actually protect secrets
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Treat state as a secret
&lt;/h3&gt;

&lt;p&gt;Because state can contain secrets, protect the whole file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keep it in a &lt;strong&gt;remote backend&lt;/strong&gt;, never in Git (&lt;code&gt;*.tfstate&lt;/code&gt; belongs in &lt;code&gt;.gitignore&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;turn on &lt;strong&gt;encryption at rest&lt;/strong&gt; (for example &lt;code&gt;encrypt = true&lt;/code&gt; on the S3 backend)&lt;/li&gt;
&lt;li&gt;give read access only to the people and pipelines that need it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Pass secrets in, don't write them down
&lt;/h3&gt;

&lt;p&gt;Never put secrets in &lt;code&gt;.tf&lt;/code&gt; or &lt;code&gt;.tfvars&lt;/code&gt; files. Set input variables from the environment instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ &lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;TF_VAR_db_password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;
$ terraform plan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In CI, inject them from the pipeline's secret store.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use ephemeral values (Terraform 1.10+)
&lt;/h3&gt;

&lt;p&gt;Ephemeral resources generate or fetch a secret &lt;strong&gt;during a run only&lt;/strong&gt;. It's never written to the plan or the state:&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;ephemeral&lt;/span&gt; &lt;span class="s2"&gt;"random_password"&lt;/span&gt; &lt;span class="s2"&gt;"db"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can't put an ephemeral value somewhere that would be persisted, like a normal resource argument. Terraform rejects it with "Invalid use of ephemeral value". That's what makes it safe.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Use write-only arguments (Terraform 1.11+)
&lt;/h3&gt;

&lt;p&gt;So how does an ephemeral value reach a real resource? Through &lt;strong&gt;write-only arguments&lt;/strong&gt;: the provider receives the value, and Terraform never stores it:&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;"aws_db_instance"&lt;/span&gt; &lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;engine&lt;/span&gt;              &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"postgres"&lt;/span&gt;
  &lt;span class="n"&gt;instance_class&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"db.t3.micro"&lt;/span&gt;
  &lt;span class="n"&gt;allocated_storage&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
  &lt;span class="n"&gt;username&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"app"&lt;/span&gt;
  &lt;span class="n"&gt;password_wo&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ephemeral&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random_password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;
  &lt;span class="n"&gt;password_wo_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# bump to rotate the password&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because Terraform can't see the stored value, you bump &lt;code&gt;password_wo_version&lt;/code&gt; to tell it you want to change the password.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Or keep secrets out of Terraform entirely
&lt;/h3&gt;

&lt;p&gt;For many teams the cleanest answer is a secrets manager, such as HashiCorp Vault or your cloud's own, that applications read at runtime. Terraform then manages the access, not the secret itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: catch risky code before it's applied
&lt;/h2&gt;

&lt;p&gt;Scanners like &lt;strong&gt;trivy&lt;/strong&gt; (&lt;code&gt;trivy config .&lt;/code&gt;, which now includes tfsec) and &lt;strong&gt;checkov&lt;/strong&gt; (&lt;code&gt;checkov -d .&lt;/code&gt;) read your Terraform code directly, with no credentials, and flag problems such as a security group that opens SSH to &lt;code&gt;0.0.0.0/0&lt;/code&gt;. Run them in CI on every pull request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;sensitive = true&lt;/code&gt; hides values in the terminal. It does &lt;strong&gt;not&lt;/strong&gt; keep them out of state&lt;/li&gt;
&lt;li&gt;Protect state: remote, encrypted, access-controlled, never in Git&lt;/li&gt;
&lt;li&gt;Pass secrets in with &lt;code&gt;TF_VAR_&lt;/code&gt; environment variables&lt;/li&gt;
&lt;li&gt;Use ephemeral values and write-only arguments to keep secrets out of state completely&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This post is based on episode 13 of &lt;strong&gt;Terraform from Zero&lt;/strong&gt;, a free, hands-on Terraform course on YouTube that covers the whole Terraform Associate (004) exam, plus real-world troubleshooting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;▶ Full course: &lt;a href="https://www.youtube.com/playlist?list=PLAp4RkKcTTLw" rel="noopener noreferrer"&gt;https://www.youtube.com/playlist?list=PLAp4RkKcTTLw&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 Code for this episode: &lt;a href="https://github.com/mrhr7/terraform-in-practice/tree/main/ep13_sensitive_data" rel="noopener noreferrer"&gt;https://github.com/mrhr7/terraform-in-practice/tree/main/ep13_sensitive_data&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>terraform</category>
      <category>devops</category>
      <category>security</category>
      <category>iac</category>
    </item>
    <item>
      <title>Terraform count vs for_each: why deleting one item rebuilt my resources</title>
      <dc:creator>DevOps in Practice</dc:creator>
      <pubDate>Thu, 03 Dec 2026 10:30:42 +0000</pubDate>
      <link>https://dev.to/devops_in_practice/terraform-count-vs-foreach-why-deleting-one-item-rebuilt-my-resources-4ko5</link>
      <guid>https://dev.to/devops_in_practice/terraform-count-vs-foreach-why-deleting-one-item-rebuilt-my-resources-4ko5</guid>
      <description>&lt;p&gt;You have three services in a list. You remove one. You expect Terraform to delete one thing.&lt;/p&gt;

&lt;p&gt;Instead the plan says it will &lt;strong&gt;replace&lt;/strong&gt; one resource and &lt;strong&gt;destroy&lt;/strong&gt; another. If those were databases or load balancers, that's an outage.&lt;/p&gt;

&lt;p&gt;This post shows exactly why that happens with &lt;code&gt;count&lt;/code&gt;, how &lt;code&gt;for_each&lt;/code&gt; avoids it, and how to switch existing code from one to the other without rebuilding anything. Every command and output below is from a real run on Terraform 1.17.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproducing the problem with count
&lt;/h2&gt;

&lt;p&gt;Three files from one list, created 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;variable&lt;/span&gt; &lt;span class="s2"&gt;"services"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"web"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"api"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"db"&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;"svc"&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="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&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;"${var.services[count.index]}.txt"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&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;After &lt;code&gt;terraform apply&lt;/code&gt;, Terraform tracks these three files by &lt;strong&gt;position&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;File&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;local_file.svc[0]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;web.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;local_file.svc[1]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;api.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;local_file.svc[2]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;db.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now remove &lt;code&gt;"api"&lt;/code&gt;, the middle item, and plan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ sed -i &lt;span class="s1"&gt;'s/"api", //'&lt;/span&gt; main.tf
$ terraform plan -no-color &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'must be|will be|Plan'&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc[1] must be replaced&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc[2] will be destroyed&lt;/span&gt;
Plan: 1 to add, 0 to change, 2 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One deletion, two changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;count&lt;/code&gt;, an instance's identity &lt;strong&gt;is its index&lt;/strong&gt;. After you remove &lt;code&gt;"api"&lt;/code&gt;, the list is &lt;code&gt;["web", "db"]&lt;/code&gt;, so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;index &lt;code&gt;1&lt;/code&gt; used to be &lt;code&gt;api&lt;/code&gt;, and is now &lt;code&gt;db&lt;/code&gt;: different content, so Terraform &lt;strong&gt;replaces&lt;/strong&gt; it&lt;/li&gt;
&lt;li&gt;index &lt;code&gt;2&lt;/code&gt; no longer exists, so Terraform &lt;strong&gt;destroys&lt;/strong&gt; it (that was the old &lt;code&gt;db&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Terraform isn't confused. It's doing exactly what the addresses say. The problem is that positions shift when the list changes, and every resource after the removed item shifts with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same thing with for_each
&lt;/h2&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;"svc"&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="nf"&gt;toset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&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;"${each.key}.txt"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now each instance is addressed by a &lt;strong&gt;key&lt;/strong&gt;: &lt;code&gt;local_file.svc["web"]&lt;/code&gt;, &lt;code&gt;local_file.svc["api"]&lt;/code&gt;, &lt;code&gt;local_file.svc["db"]&lt;/code&gt;. Remove &lt;code&gt;"api"&lt;/code&gt; again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ terraform plan -no-color &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'must be|will be|Plan'&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc["api"] will be destroyed&lt;/span&gt;
Plan: 0 to add, 0 to change, 1 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only &lt;code&gt;api&lt;/code&gt; goes. &lt;code&gt;web&lt;/code&gt; and &lt;code&gt;db&lt;/code&gt; keep their addresses, so nothing else moves.&lt;/p&gt;

&lt;h2&gt;
  
  
  When count is still the right tool
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;count&lt;/code&gt; isn't wrong, it's just positional. It's a good fit when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you want an &lt;strong&gt;on/off switch&lt;/strong&gt;: &lt;code&gt;count = var.enable_monitoring ? 1 : 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;you create &lt;strong&gt;identical copies&lt;/strong&gt; where it never matters which one is removed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a collection of &lt;em&gt;different&lt;/em&gt; things, such as services, users, buckets or subnets, prefer &lt;code&gt;for_each&lt;/code&gt; with a map or a set of strings.&lt;/p&gt;

&lt;p&gt;One rule to remember: &lt;code&gt;for_each&lt;/code&gt; keys must be &lt;strong&gt;known at plan time&lt;/strong&gt;. Use names you write in code or in variables, not IDs that only exist after apply. (That mistake has its own error, &lt;code&gt;Invalid for_each argument&lt;/code&gt;, which I cover in a separate post.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Migrating from count to for_each without a rebuild
&lt;/h2&gt;

&lt;p&gt;If you just change &lt;code&gt;count&lt;/code&gt; to &lt;code&gt;for_each&lt;/code&gt; in existing code, Terraform sees new addresses and plans to destroy and recreate everything. Tell it the instances moved instead, with one &lt;code&gt;moved&lt;/code&gt; block per instance. Here's the list example from above, migrated:&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;"svc"&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="nf"&gt;toset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# was: count = length(var.services)&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;"${each.key}.txt"&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&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;svc&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;svc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"web"&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;svc&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;svc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"api"&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;svc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&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;svc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"db"&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ terraform plan -no-color &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'moved|Plan'&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc[1] has moved to local_file.svc["api"]&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc[2] has moved to local_file.svc["db"]&lt;/span&gt;
  &lt;span class="c"&gt;# local_file.svc[0] has moved to local_file.svc["web"]&lt;/span&gt;
Plan: 0 to add, 0 to change, 0 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Map each index to the key of the item that was really at that position. If you get one wrong, the plan shows &lt;code&gt;must be replaced&lt;/code&gt; instead of &lt;code&gt;has moved&lt;/code&gt;, so read it before you apply.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep those blocks in the code afterwards&lt;/strong&gt;: anyone (or any environment) that hasn't applied the change yet still needs them, and deleting a &lt;code&gt;moved&lt;/code&gt; block is a breaking change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; identifies instances by &lt;strong&gt;position&lt;/strong&gt;, so removing an item shifts everything after it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for_each&lt;/code&gt; identifies instances by &lt;strong&gt;key&lt;/strong&gt;, so removing an item only removes that item&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;count&lt;/code&gt; for on/off switches and identical copies, &lt;code&gt;for_each&lt;/code&gt; for collections&lt;/li&gt;
&lt;li&gt;Migrate existing code with &lt;code&gt;moved&lt;/code&gt; blocks, and keep them&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This post is based on episode 10 of &lt;strong&gt;Terraform from Zero&lt;/strong&gt;, a free, hands-on Terraform course on YouTube that covers the whole Terraform Associate (004) exam, plus real-world troubleshooting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;▶ Full course: &lt;a href="https://www.youtube.com/playlist?list=PLAp4RkKcTTLw" rel="noopener noreferrer"&gt;https://www.youtube.com/playlist?list=PLAp4RkKcTTLw&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 Code for this episode: &lt;a href="https://github.com/mrhr7/terraform-in-practice/tree/main/ep10_count_and_for_each" rel="noopener noreferrer"&gt;https://github.com/mrhr7/terraform-in-practice/tree/main/ep10_count_and_for_each&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

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