title: Why terraform is unable to compute a local variable correctly in the following trivial scenario?
body: <p>Given is the following configuration (main.tf):</p>
  
  <pre><code>locals {
      locations = toset(["a", "b"])
  }
  
  resource "local_file" "instance" {
      for_each = local.locations
  
      content  = each.value
      filename = "${path.module}/${each.value}.txt"
  }
  
  output "primary_filename" {
      value = local_file.instance["a"].filename
  }
  </code></pre>
  
  <p>And it seems to work fine:</p>
  
  <pre><code>C:\work\test&gt; dir
  
  
      Directory: C:\work\test
  
  
  Mode                LastWriteTime         Length Name
  ----                -------------         ------ ----
  -a----        4/15/2020  11:47 PM            280 main.tf
  
  
  C:\work\test&gt; terraform init
  
  Initializing the backend...
  
  Initializing provider plugins...
  - Checking for available provider plugins...
  - Downloading plugin for provider "local" (hashicorp/local) 1.4.0...
  
  ...
  C:\work\test&gt; terraform apply -auto-approve
  local_file.instance["b"]: Creating...
  local_file.instance["a"]: Creating...
  local_file.instance["a"]: Creation complete after 0s [id=86f7e437faa5a7fce15d1ddcb9eaeaea377667b8]
  local_file.instance["b"]: Creation complete after 0s [id=e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98]
  
  Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  
  Outputs:
  
  primary_filename = ./a.txt
  C:\work\test&gt;
  </code></pre>
  
  <p>Now I delete the file <code>a.txt</code> and rerun:</p>
  
  <pre><code>C:\work\test&gt; del .\a.txt
  C:\work\test&gt; terraform apply -auto-approve
  local_file.instance["a"]: Refreshing state... [id=86f7e437faa5a7fce15d1ddcb9eaeaea377667b8]
  local_file.instance["b"]: Refreshing state... [id=e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98]
  
  Error: Invalid index
  
    on main.tf line 13, in output "primary_filename":
    13:     value = local_file.instance["a"].filename
      |----------------
      | local_file.instance is object with 1 attribute "b"
  
  The given key does not identify an element in this collection value.
  </code></pre>
  
  <p>It can be fixed by using the <code>try</code> function:</p>
  
  <pre><code>    value = try(local_file.instance["a"].filename, "")
  </code></pre>
  
  <p>Which does make it work:</p>
  
  <pre><code>C:\work\test&gt; terraform apply -auto-approve
  local_file.instance["b"]: Refreshing state... [id=e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98]
  local_file.instance["a"]: Refreshing state... [id=86f7e437faa5a7fce15d1ddcb9eaeaea377667b8]
  local_file.instance["a"]: Creating...
  local_file.instance["a"]: Creation complete after 0s [id=86f7e437faa5a7fce15d1ddcb9eaeaea377667b8]
  
  Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  
  Outputs:
  
  primary_filename = ./a.txt
  C:\work\test&gt;
  </code></pre>
  
  <p>Now I know we are not supposed to delete resources outside of terraform, but things happen and my expectation is that terraform handles it gracefully. And it does, except for this local variable behavior.</p>
  
  <p>I do not like using the <code>try</code> function, because it would hide a real problem. Ideally, it should behave like <code>try</code> during the plan phase and without <code>try</code> during the apply phase.</p>
  
  <p>Anyway, I have a feeling I am missing something important here, like I am not using the local variables correctly or something else. So, what am I missing?</p>