title: Terraform - Iterate over a map of Objects in a Template to generate different yaml files
body: <p>I need to generate around 50 YAML files. Most of the content in the YAML is constant, only <code>name</code>, and <code>port</code> changes. I am using <code>Terraform v1.2.2</code>. I request you to help me correct my issue or suggest an alternate idea/solution achieve my goal</p>
  <p><code>netwrokfile.yaml</code> after generation</p>
  <pre><code>    apiVersion: projectcalico.org/v3
      kind: GlobalNetworkPolicy
      metadata:
        name: web-policy # Changes
      spec:
        action: allow    
        rules: 
        - to:
          - operation:
              ports: [&quot;9080&quot;, &quot;8080&quot;] # Changes from each application may have one or two or three ports.
      selector:
        matchLabels:
          app: web-policy # Changes
  </code></pre>
  <p><code>networkfile.yaml.tpl</code></p>
  <pre><code>    apiVersion: projectcalico.org/v3
      kind: GlobalNetworkPolicy
      metadata:
      %{~ for sName, sPort in zipmap(sName, sPort) ~}
        name: ${sName}
      spec:
        action: allow
        rules:
        - to:
           - operation:
             ports: ${sPort}
      selector:
        matchLabels:
          app: ${sName}
      %{~ endfor ~}
  </code></pre>
  <p><code>main.tf</code> in my module</p>
  <pre><code>resource &quot;kubernetes_manifest&quot; &quot;istio-config&quot; {
     manifest = templatefile(&quot;${path.module}/templates/networkfile.yaml.tpl&quot;,
     {
       sName =  [for v in var.serviceName: v.sName]
       sPort =  [for v in var.serviceName: v.sPort]
     }
      )
    }
  </code></pre>
  <p><code>main.tf</code> calling my module</p>
  <pre><code>module &quot;authorization_policy&quot; {
    source = &quot;../../modules/GlobalNetworkPolicy&quot;
  
    serviceName = var.serviceName
  }
  </code></pre>
  <p>Myy <code>variable.tf</code></p>
  <pre><code>variable &quot;serviceName&quot; {
  type = map(object({
    sName  = string
    sPort = list(string)
  }))
  default = {
    test-vm1 = {
      sName  = &quot;web-policy&quot;
      sPort = [&quot;8080&quot;, &quot;8081&quot;]
    },
    test-vm2 = {
      sName  = &quot;db-policy&quot;
      sPort = [&quot;8080&quot;, &quot;8081&quot;, &quot;443&quot;]
    }
  }
  }
  </code></pre>
  <p>With the above code, I get the below error</p>
  <pre><code>can't unmarshal tftypes.String into *map[string]tftypes.Value, expected map[string]tftypes.Value
  </code></pre>
  <p>I tried using the solution mentioned here (<a href="https://stackoverflow.com/questions/64651270/terraform-iterate-over-a-list-of-objects-in-a-template">Terraform - Iterate over a List of Objects in a Template</a>). But it didn't help and it gives me the same error.</p>
  <p>I also tried (<a href="https://www.terraform.io/language/functions/templatefile" rel="nofollow noreferrer">https://www.terraform.io/language/functions/templatefile</a>) it give me a completely different error</p>
  <pre><code>Call to function &quot;templatefile&quot; failed: ../../modules/GlobalNetworkPolicy/templates/networkfile.yaml.tpl:5,3-4: Unsupported operator; Bitwise operators are not supported. Did you mean boolean NOT (&quot;!&quot;)?,
  │ and 4 other diagnostic(s).
  </code></pre>
  <p>Below is the complete error - Fixed by adding yamlencode</p>
  <pre><code>╷
  │ Error: Failed to extract &quot;manifest&quot; attribute value from resource configuration
  │
  │   with module.authorization_policy.kubernetes_manifest.istio-config,
  │   on ../../modules/GlobalNetworkPolicy/main.tf line 2, in resource &quot;kubernetes_manifest&quot; &quot;istio-config&quot;:
  │    2:    manifest = templatefile(&quot;${path.module}/templates/networkfile.yaml.tpl&quot;,
  │    3:    {
  │    4:      serviceName = var.serviceName
  │    5:    }
  │    6:     )
  │
  │ can't unmarshal tftypes.String into *map[string]tftypes.Value, expected map[string]tftypes.Value
  </code></pre>