title: Iterate through list of dictionaries to create terraform resources with different values
body: <p><strong>Goal</strong>
  Create two <code>aws_athena_named_query</code> resources using terraform. The configuration for each query is defined in separate dictionaries. All of the query dictionaries are nested in a list (see <code>query_dict</code> variable).</p>
  <pre><code>variable query_dict {
      default = [
          {
              name = &quot;query1&quot;
              workgroup = &quot;bar&quot;
              query = &quot;SELECT * FROM foo&quot;   
          },
          {
              name = &quot;query2&quot;
              workgroup = &quot;bar&quot;
              query = &quot;SELECT * FROM baz&quot;   
          }
      ]
  }
  
  resource &quot;aws_athena_named_query&quot; &quot;olap&quot; {
    for_each = toset([for query in var.query_dict: {
              name   = query.name
              workgroup = query.workgroup
              query = query.query}])
    name = each.value.name
    query = each.value.query
    database = &quot;test&quot;
    workgroup = each.value.workgroup
  }
  </code></pre>
  <p>Which results in the error:</p>
  <pre><code>The given &quot;for_each&quot; argument value is unsuitable: &quot;for_each&quot; supports maps
  and sets of strings, but you have provided a set containing type object.
  
  </code></pre>
  <p><strong>Attempt 2:</strong></p>
  <pre><code>resource &quot;aws_athena_named_query&quot; &quot;olap&quot; {
    for_each = [for query in tomap(var.query_dict): {
              name   = query.name
              workgroup = query.workgroup
              query = query.query}]
    name = each.value.name
    query = each.value.query
    database = &quot;test&quot;
    workgroup = each.value.workgroup
  } 
  </code></pre>
  <p>Which results in:</p>
  <pre><code>Invalid value for &quot;v&quot; parameter: cannot convert tuple to map of any single
  type.
  </code></pre>
  <p><strong>Attempt 3:</strong></p>
  <pre><code>resource &quot;aws_athena_named_query&quot; &quot;olap&quot; {
    count = length(var.query_dict)
    name = query_dict[count.index][&quot;name&quot;]
    query = query_dict[count.index][&quot;query&quot;]
    database = &quot;test&quot;
    workgroup = query_dict[count.index][&quot;workgroup&quot;]
  }
  </code></pre>
  <p>Which outputs the error:</p>
  <pre><code>  on main.tf line 57, in resource &quot;aws_athena_named_query&quot; &quot;olap&quot;:
    57:   workgroup = query_dict[count.index][&quot;workgroup&quot;]
  
  A reference to a resource type must be followed by at least one attribute
  access, specifying the resource name.
  
  </code></pre>
  <p><strong>Expected Output:</strong></p>
  <pre><code># aws_athena_named_query.olap[&quot;query1&quot;] will be created
    + resource &quot;aws_athena_named_query&quot; &quot;olap&quot; {
        + database  = &quot;db&quot;
        + id        = (known after apply)
        + name      = &quot;query1&quot;
        + query     = &quot;SELECT * FROM foo&quot;
        + workgroup = &quot;bar&quot;
      }
  
  # aws_athena_named_query.olap[&quot;query2&quot;] will be created
    + resource &quot;aws_athena_named_query&quot; &quot;olap&quot; {
        + database  = &quot;db&quot;
        + id        = (known after apply)
        + name      = &quot;query2&quot;
        + query     = &quot;SELECT * FROM baz&quot;
        + workgroup = &quot;bar&quot;
      }
  </code></pre>