title: terraform temporary/intermediate variables in loops?
body: <p>I would like to know if it is possible to use temporary variables inside a for loop for intermediate computation. If yes, what is the syntax to use that, the example is simplified just to illustrate the problem. This example code works. I've commented the portion which doesn't work, but would like to know how to use intermediate variable. Thanks!</p>
  <pre><code>
  locals {
    nestedmap = &lt;&lt;-EOT
    user-john:
      db:  &quot;db34&quot;
      tables:
        t1:
          rows: 4
        t2:
          rows: 8
    user-chris:
      db:  &quot;db22&quot;
      tables:
        t1:
          rows: 3
        t2:
          rows: 7
    user-mary:
      db:  &quot;db78&quot;
      tables:
        t1:
          rows: 2
        t2:
          rows: 10
    EOT
  
    flatlist = flatten([
      for ux_key, ux_val in yamldecode(local.nestedmap): [
        for tx_key, tx_val in ux_val.tables: {
          pfx = trimprefix(ux_key, &quot;user-&quot;)
          #key = &quot;${pfx}-${tx_key}&quot;
          key = &quot;${trimprefix(ux_key, &quot;user-&quot;)}-${tx_key}&quot;
          db = ux_val.db
          rows = tx_val.rows
        }
      ]
    ])
    flatmap = {for fx in local.flatlist:  fx.key =&gt; fx}
  }
  </code></pre>