<p>I have a requirement to execute multiple Kubernetes manifests of the same kind. Just names, action, and port changes</p>
<p>my <code>networkpolicy.yaml.tpl</code></p>
<pre><code>${yamlencode(
    apiVersion: projectcalico.org/v3
    kind: GlobalNetworkPolicy
    metadata:
      name: ${name}-policy
    spec:
      action: ${action}
      rules: 
      - to:
        - operation:
            ports: [for port in ports : &quot;${port}&quot;]
)}
    
</code></pre>
<p>values that need to be populated for each microservice</p>
<pre><code>| --------  | -------- | --------------| --------------|
| name      | frontend | backend       | middleware    |
| action    | allow    | allow         | allow         |
| ports     | 8080,443 | 4731          | 8751,7542     |
</code></pre>
<p>Example <code>networkpolicy.yaml</code> after generation</p>
<pre><code>    apiVersion: projectcalico.org/v3
    kind: GlobalNetworkPolicy
    metadata:
      name: frontend-policy
    spec:
      action: allow
      rules: 
      - to:
        - operation:
            ports: [&quot;8080&quot;, &quot;443&quot;]
</code></pre>
<p>how can I achieve this? I am not clear as how-to write <code>main.tf</code></p>
<pre><code>    resource &quot;kubernetes_manifest&quot; &quot;istio-config&quot; {
      manifest = yamldecode(templatefile(&quot;${path.module}/networkpolicy.yaml.tpl&quot;, {
        name     =
        action   = 
        port     =  
    }))
  }
</code></pre>

Eva
<p>You can use the <code>loop</code></p>
<p><strong>Example</strong></p>
<pre><code>menifest = yamldecode(templatefile(&quot;${path.module}/networkpolicy.yaml.tpl&quot;, {
    name = var.name
  })
</code></pre>
<p>update the <strong>networkpolicy.yaml.tpl</strong> file</p>
<pre><code>%{ for s in nameservers ~}
name ${s}
%{ endfor ~}
</code></pre>
<p>If you don't want to edit the tpl file you can edit the main.tf directly</p>
<pre><code>name = &lt;&lt;-EOT
    %{ for s in var.name ~}
    name ${s}
    %{ endfor ~}
  EOT
</code></pre>
<p>Ref : <a href="https://www.terraform.io/language/functions/templatefile" rel="nofollow noreferrer">https://www.terraform.io/language/functions/templatefile</a></p>
<p>Read more about simple loop : <a href="https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9" rel="nofollow noreferrer">https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9</a></p>
<p>You can use the <strong>count</strong> or <strong>for_each</strong> loop to simple make it possible.</p>
