title: Terraform 0.12 nested for loops
body: <p>I am trying to implement nested for loops using Terraform 0.12's new features in order to loop through AWS IAM users, each of which can have one or more policies attached. The variable used to represent this list is of type map(list(string)) and looks something like this:</p>
  
  <pre><code>{
    "user 1" = [ "policy1", "policy2" ],
    "user 2" = [ "policy1" ]
  }
  </code></pre>
  
  <p>Getting the list of users to create is easy enough via <code>keys()</code>, but since there is currently no mechanism for nesting looped resource creation in Terraform, the policy attachments have to happen as a singular loop independent of each user. So, I am attempting to construct a list of user:policy associations from the map input that would look something like this based on the example above:</p>
  
  <pre><code>[
    [ "user1", "policy1" ],
    [ "user1", "policy2" ],
    [ "user2", "policy1" ]
  ]
  </code></pre>
  
  <p>I am attempting construct that list and store it in a local variable like so, where <code>var.iam-user-policy-map</code> is the input map:</p>
  
  <pre><code>locals {
    ...
    association-list = [
      for user in keys(var.iam-user-policy-map):
      [
        for policy in var.iam-user-policy-map[user]:
        [user, policy]
      ]
    ]
    ...
  }
  </code></pre>
  
  <p>However, I am getting errors when attempting to access the values in that nested list. I am trying to access the user portion of the association with the reference <code>local.association-list[count.index][0]</code> and the policy with <code>local.association-list[count.index][1]</code>, but on running <code>terraform plan</code> it errors out:</p>
  
  <pre><code>Error: Incorrect attribute value type
  
    on main.tf line 27, in resource "aws_iam_user_policy_attachment" "test-attach":
    27:   user = local.association-list[count.index][0]
  
  Inappropriate value for attribute "user": string required.
  
  
  Error: Incorrect attribute value type
  
    on main.tf line 27, in resource "aws_iam_user_policy_attachment" "test-attach":
    27:   user = local.association-list[count.index][0]
  
  Inappropriate value for attribute "user": string required.
  
  
  Error: Invalid index
  
    on main.tf line 28, in resource "aws_iam_user_policy_attachment" "test-attach":
    28:   policy_arn = "arn:aws-us-gov:iam::aws:policy/${local.association-list[count.index][1]}"
      |----------------
      | count.index is 0
      | local.association-list is tuple with 2 elements
  
  The given key does not identify an element in this collection value.
  
  
  Error: Invalid template interpolation value
  
    on main.tf line 28, in resource "aws_iam_user_policy_attachment" "test-attach":
    28:   policy_arn = "arn:aws-us-gov:iam::aws:policy/${local.association-list[count.index][1]}"
      |----------------
      | count.index is 1
      | local.association-list is tuple with 2 elements
  
  Cannot include the given value in a string template: string required.
  </code></pre>
  
  <p>What am I doing wrong?</p>