title: Terraform 0.12 - Produce map/object from nested for loop
body: <p>As a follow up to <a href="https://stackoverflow.com/questions/56047306/terraform-0-12-nested-for-loops/57280420#57280420">Terraform 0.12 nested for loops</a>. I am trying to produce an object out of a nested loop but failing miserably :(</p>
  
  <p>How would you go about producing:</p>
  
  <pre><code>  Outputs:
  
    association-list = {
      "policy1" = "user1"
      "policy2" = "user1"
      "policy2" = "user2"
    }
  </code></pre>
  
  <p>From:</p>
  
  <pre><code>iam-policy-users-map = {
    "policy1" = [ "user1" ]
    "policy2" = [ "user1", "user2" ]
  }
  </code></pre>
  
  <p>I have tried many variations of:</p>
  
  <pre><code>variable iam-policy-users-map {
    default = {
      "policy1" = [ "user1" ]
      "policy2" = [ "user1", "user2" ]
    }
  }
  
  locals {
    association-map = merge({
      for policy, users in var.iam-policy-users-map : {
        for user in users : {
          policy =&gt; user
        }
      }
    })
  
  output association-map {
    value = local.association-map
  }
  </code></pre>
  
  <p>with zero success so far. Only managed to get the following depending on the variation:</p>
  
  <blockquote>
    <p>Error: Invalid 'for' expression.
    Extra characters after the end of the 'for' expression.</p>
    
    <p>Error: Missing attribute value.
    Expected an attribute value, introduced by an equals sign ("=").</p>
    
    <p>Error: Invalid 'for' expression. 
    Key expression is required when building an object.</p>
    
    <p>Error: Missing key/value separator. Expected an equals sign ("=") to
    mark the beginning of the attribute value.</p>
  </blockquote>
  
  <p>For reference, the following code is however capable of producing a list of maps:</p>
  
  <pre><code>variable iam-policy-users-map {
    default = {
      "policy1" = [ "user1" ]
      "policy2" = [ "user1", "user2" ]
    }
  }
  
  locals {    
    association-list = flatten([
      for policy, users in var.iam-policy-users-map : [
        for user in users : {
          user   = user
          policy = policy
        }
      ]
    ])
  }
  
  output association-list {
    value = local.association-list
  }
  </code></pre>
  
  <blockquote>
    <p>Outputs:</p>
    
    <p>association-list = [   {
        "policy" = "policy1"
        "user" = "user1"   },   {
        "policy" = "policy2"
        "user" = "user1"   },   {
        "policy" = "policy2"
        "user" = "user2"   }, ]</p>
  </blockquote>