title: Terraform 0.12 Creating ingress rules with template
body: <p>I wonder if something like this could be used. I have functional code working, but as pods in kubernetes will grow up fast, I want to convert into templates. This is an example for the creation of each nginx ingress rule for each wordpress site pod.</p>
  
  <p>Now, each pod has its wordpress ingress entry with:</p>
  
  <pre><code>resource "kubernetes_ingress" "ingress_nginx_siteA" {
    metadata {
      name        = "ingress-nginx-siteA"
      namespace   = "default"
      annotations = { "kubernetes.io/ingress.class" = "nginx", "nginx.ingress.kubernetes.io/configuration-snippet" = "modsecurity_rules '\n SecRuleEngine On\n SecRequestBodyAccess On\n SecAuditEngine RelevantOnly\n SecAuditLogParts ABCIJDEFHZ\n SecAuditLog /var/log/modsec_audit.log\n SecRuleRemoveById 932140\n';\n", "nginx.ingress.kubernetes.io/ssl-passthrough" = "true" }
    }
    spec {
      tls {
        hosts       = ["siteA.test.com"]
        secret_name = "wildcard-test-com"
      }
      rule {
        host = "siteA.test.com"
        http {
          path {
            path = "/"
            backend {
              service_name = "siteA"
              service_port = "80"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Now I want to split into <strong>variables.tf</strong> that contain the whole sites variables, a template file <strong>rules.tpl</strong> and the <strong>main.tf</strong> that orchestrate this stuff.</p>
  
  <p>variables.tf:</p>
  
  <pre><code>variable "wordpress_site" {
    type = map(object({
      name        = string
      url         = string
      certificate = string
    }))
      default = {
      siteA = {
        name        = siteA
        url         = siteA.test.com
        certificate = wildcard-test-com
      }
      siteB = {
        name        = siteB
        url         = siteB.test.com
        certificate = wildcard-test-com
      }
    }
  } 
  
  </code></pre>
  
  <p>rules.tpl:</p>
  
  <pre><code>%{ for name in wordpress_site.name ~}
  resource "kubernetes_ingress" "ingress_nginx_${name}" {
    metadata {
      name        = "ingress-nginx-${name}"
      namespace   = "default"
      annotations = { "kubernetes.io/ingress.class" = "nginx", "nginx.ingress.kubernetes.io/configuration-snippet" = "modsecurity_rules '\n SecRuleEngine On\n SecRequestBodyAccess On\n SecAuditEngine RelevantOnly\n SecAuditLogParts ABCIJDEFHZ\n SecAuditLog /var/log/modsec_audit.log\n SecRuleRemoveById 932140\n';\n", "nginx.ingress.kubernetes.io/ssl-passthrough" = "true" }
    }
    spec {
      tls {
        hosts       = ["${wordpress_site.url}"]
        secret_name = "${wordpress_site.certificate}"
      }
      rule {
        host = "${wordpress_site.url}"
        http {
          path {
            path = "/"
            backend {
              service_name = "${name}"
              service_port = "80"
            }
          }
        }
      }
    }
  }
  %{ endfor ~}
  </code></pre>
  
  <p>and now, in main.tf, what is the best way in order to mix it all? I see that new functionalities are added in TF 0.12 like templatefile function, but didn't know at all if I can use it like:</p>
  
  <p>main.tf:</p>
  
  <pre><code>templatefile(${path.module}/rules.tpl, ${module.var.wordpress_site})
  
  </code></pre>
  
  <p>Thanks all for your support!</p>