<p><strong>What I am trying to do?</strong></p>
<p>I want to create a <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/service" rel="nofollow noreferrer">kubernetes service</a> object using terraform, but make it re-usable. So everytime there's a new service, I could just append the variables.</p>
<p><strong>My problem:</strong></p>
<p>I have been reading and trying different things but I am not sure how would I loop over &quot;annotations&quot; and &quot;selectors&quot; which will have more than one key value pair.</p>
<p><strong>Code Example</strong></p>
<p>Variables.tf using which I want to build the actual terraform resource.</p>
<pre><code>variable &quot;apps&quot; {
  default = {
    &quot;app1&quot; = {
      svc_name  = &quot;app1&quot;
      namespace = &quot;testns&quot;
      annotations = {
        &quot;testannotation&quot; = &quot;ann1&quot;
      }
      selector = {
        app = &quot;podinfo&quot;
        env = &quot;dev&quot;
      }
      ports = {
        name       = &quot;http&quot;
        port       = 80
        protocol   = &quot;TCP&quot;
        targetPort = 8008
      }
    },
    &quot;app2&quot; = {
      svc_name  = &quot;app2&quot;
      namespace = &quot;testns&quot;
      annotations = {
        &quot;testannotation&quot; = &quot;ann1&quot;
      }
      selector = {
        app = &quot;someapp&quot;
        env = &quot;qa&quot;
      }
      ports = {
        name       = &quot;http&quot;
        port       = 8080
        protocol   = &quot;TCP&quot;
        targetPort = 8080
      }
    },
  }
}
</code></pre>
<p>Here is the main.tf where I want to loop over &quot;annotations&quot; and &quot;selectors&quot; of the variables because there could be more than one of them. BUT there is only one &quot;annotations&quot; and &quot;selector&quot; block. So I couldn't use &quot;dynamic&quot; as it will generate many of those blocks.</p>
<pre><code>resource &quot;kubernetes_service&quot; &quot;service&quot; {
  for_each = var.apps
  metadata {
    name      = each.value.svc_name
    namespace = each.value.namespace
    # annotations = {
    #   HOW DO I GET THE ANNOTATIONS HERE
    # }
  }
  spec {
    selector = {
      ## HOW DO I GET THE SELECTORS FROM VARIABLEES HERE
    }
    session_affinity = &quot;ClientIP&quot;
    port {
      port        = each.value.ports.port
      target_port = each.value.ports.targetPort
    }

    type = &quot;ClusterIP&quot;
  }
}
</code></pre>
<p>I'd appreciate any guide, links or suggestions here!</p>

<p>You do the same as with other properties:</p>
<pre><code>resource &quot;kubernetes_service&quot; &quot;service&quot; {
  for_each = var.apps
  metadata {
    name      = each.value.svc_name
    namespace = each.value.namespace
    annotations = each.value.annotations 
  }
  spec {
    selector = each.value.selector 
    session_affinity = &quot;ClientIP&quot;
    port {
      port        = each.value.ports.port
      target_port = each.value.ports.targetPort
    }

    type = &quot;ClusterIP&quot;
  }
}
</code></pre>
