title: Terraform iterate nested objects and lists
body: <p>I am using Terraform to setup some infrastructure on AWS. The input is a Map of objects and has stages. The code looks like this:</p>
  <p>variables.tf</p>
  <pre><code>variable &quot;aws_api_gw_instances&quot; {
   description = &quot;Maps of objects representing API GW instances with id and stages&quot;
   type = map(object({
      stages  = list(string)
    }))
    default = {
      apiId = {
          stages = [&quot;stage1&quot;]
      }
    }
  }
  </code></pre>
  <p>Then I basically need to iterate all the objects in the map and their list of stages, effectively creating a nested loops. However I fail to understand how to achive this with Terraform. I have the following code but using this for syntax does not seem to work at all.</p>
  <pre><code>resource &quot;aws_api_gateway_deployment&quot; &quot;deploy_api&quot; {
    for apiGw in var.aws_api_gw_instances
      for stage in toset(apiGw.value[&quot;stages&quot;])
        rest_api_id = apiGw.key
        stage_name = stage
        variables = {
          tf_force_redeployment_id = local.current_time
        }
        lifecycle {
          create_before_destroy = true
        }
  }
  </code></pre>
  <p>I have previously been using the for_each syntax to iterate the map, but I do not understand how to write a nestled loop here:</p>
  <pre><code>for_each = (var.aws_api_gw_instances)  
    rest_api_id = each.key
  </code></pre>