title: Terraform - Iterate over a List of Objects in a Template
body: <p>I'm having issues iterating over a list of objects within a template interpreted by the <code>templatefile</code> function.</p>
  <p>I have the following var:</p>
  <pre><code>variable &quot;destinations&quot; {
    description = &quot;A list of EML Channel Destinations.&quot;
  
    type = list(object({
      id  = string
      url = string
    }))
  }
  </code></pre>
  <p>This is passed in to the <code>templatefile</code> function as <code>destinations</code>. The snippet of template relevant is this:</p>
  <pre><code>Destinations:
    %{ for dest in destinations ~}
    - Id: ${dest.id}
      Settings:
        URL: ${dest.url}
    %{ endfor }
  </code></pre>
  <p>When planning Terraform this gives an error of:</p>
  <pre><code>Error: &quot;template_body&quot; contains an invalid YAML: yaml: line 26: did not find expected key
  </code></pre>
  <p>I have tried switching the template code to the following:</p>
  <pre><code>Destinations:
    %{ for id, url in destinations ~}
    - Id: ${id}
      Settings:
        URL: ${url}
    %{ endfor }
  </code></pre>
  <p>Which gives a different error:</p>
  <pre><code>Call to function &quot;templatefile&quot; failed:
  ../../local-tfmodules/eml/templates/eml.yaml.tmpl:25,20-23: Invalid template
  interpolation value; Cannot include the given value in a string template:
  string required., and 2 other diagnostic(s).
  
  [!] something went wrong when creating the environment TF plan
  </code></pre>
  <p>I get the impression my iterating over the data type here is somehow incorrect but I cannot fathom how and I cannot find any docs about this at all.</p>
  <p>Here is a cut down example of how I'm calling this module:</p>
  <pre><code>module &quot;eml&quot; {
    source = &quot;../../local-tfmodules/eml&quot;
  
    name = &quot;my_eml&quot;
  
    destinations = [
      {
        id  = &quot;6&quot;
        url = &quot;https://example.com&quot;
      },
      {
        id  = &quot;7&quot;
        url = &quot;https://example.net&quot;
      }
    ]
  &lt;cut&gt;
  }
  </code></pre>