<p>I am trying to create a module in Terraform to create the basic resources in a Kubernetes cluster, this means a <code>cert-manager</code>, <code>ingress-nginx</code> (as the ingress controller) and a <code>ClusterIssuer</code> for the certificates. In this exact order.</p>
<p>The first two I am installing with a <code>helm_release</code> resource and the <code>cluster_issuer</code> via <code>kubernetes_manifest</code>.</p>
<p>I am getting the below error, which, after some Google searches, I found out that it's because the <code>cert-manager</code> installs the CRDs that the <code>ClusterIssuer</code> requires but at the <code>terraform plan</code> phase, since they are not installed yet, the manifest cannot detect the <code>ClusterIssuer</code>.</p>
<p>Then, I would like to know if there's a way to circumvent this issue but still create everything in the same configuration with only one <code>terraform apply</code>?</p>
<p>Note: I tried to use the depends_on arguments and also include a <code>time_sleep</code> block but it's useless because nothing is installed in the plan and that's where it fails</p>
<pre><code>| Error: Failed to determine GroupVersionResource for manifest
│ 
│   with module.k8s_base.kubernetes_manifest.cluster_issuer,
│   on ../../modules/k8s_base/main.tf line 37, in resource &quot;kubernetes_manifest&quot; &quot;cluster_issuer&quot;:
│   37: resource &quot;kubernetes_manifest&quot; &quot;cluster_issuer&quot; {
│ 
│ no matches for kind &quot;ClusterIssuer&quot; in group &quot;cert-manager.io&quot;
</code></pre>
<pre><code>resource &quot;helm_release&quot; &quot;cert_manager&quot; {
  chart      = &quot;cert-manager&quot;
  repository = &quot;https://charts.jetstack.io&quot;
  name       = &quot;cert-manager&quot;

  create_namespace = var.cert_manager_create_namespace
  namespace        = var.cert_manager_namespace

  set {
    name  = &quot;installCRDs&quot;
    value = &quot;true&quot;
  }
}

resource &quot;helm_release&quot; &quot;ingress_nginx&quot; {
  name = &quot;ingress-nginx&quot;

  repository = &quot;https://kubernetes.github.io/ingress-nginx&quot;
  chart      = &quot;ingress-nginx&quot;

  create_namespace = var.ingress_nginx_create_namespace
  namespace        = var.ingress_nginx_namespace

  wait = true

  depends_on = [
    helm_release.cert_manager
  ]
}

resource &quot;time_sleep&quot; &quot;wait&quot; {
  create_duration = &quot;60s&quot;

  depends_on = [helm_release.ingress_nginx]
}

resource &quot;kubernetes_manifest&quot; &quot;cluster_issuer&quot; {
  manifest = {
    &quot;apiVersion&quot; = &quot;cert-manager.io/v1&quot;
    &quot;kind&quot;       = &quot;ClusterIssuer&quot;
    &quot;metadata&quot; = {
      &quot;name&quot; = var.cluster_issuer_name
    }
    &quot;spec&quot; = {
      &quot;acme&quot; = {
        &quot;email&quot; = var.cluster_issuer_email
        &quot;privateKeySecretRef&quot; = {
          &quot;name&quot; = var.cluster_issuer_private_key_secret_name
        }
        &quot;server&quot; = var.cluster_issuer_server
        &quot;solvers&quot; = [
          {
            &quot;http01&quot; = {
              &quot;ingress&quot; = {
                &quot;class&quot; = &quot;nginx&quot;
              }
            }
          }
        ]
      }
    }
  }
  depends_on = [helm_release.cert_manager, helm_release.ingress_nginx, time_sleep.wait]
}
</code></pre>

everspader
<p><a href="https://cert-manager.io/docs/installation/helm/#option-1-installing-crds-with-kubectl" rel="nofollow noreferrer">Official documentation</a> says to use <code>kubectl apply</code> before installing this with a helm chart, making it a two step process.  Using Terraform, this would make it a 3 step process in that you have to apply a targeted section to create the cluster so you can have access to kubeconfig credentials, then run the kubectl apply command to install the CRDs, and finally run terraform apply again to install the helm chart and the rest of the IaC. This is even less ideal.</p>
<p>I would use the <code>kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.8.0/cert-manager.crds.yaml</code> in kubectl_manifest resources as the comment above suggests, but this is impossible since this does not link to a single yaml file but so many of them one would not be able to keep up with the changes. Unfortunately, there is no &quot;kubectl_apply&quot; terraform resource** for the helm chart to depend on those CRDs being installed first.</p>
<p>Despite all this wonkiness, there is a solution, and that is to use the helm_release resource twice. It requires creating a module and referencing a custom helm chart for the cert-issuer. It's not ideal given the amount of effort that has to be used to create it for custom needs, but once it's created, it's a reusable, modular solution.</p>
<pre><code>#
# Cert-manager
# main.tf
#
resource &quot;helm_release&quot; &quot;cert_manager&quot; {
  name             = &quot;cert-manager&quot;
  repository       = &quot;https://charts.jetstack.io&quot;
  chart            = &quot;cert-manager&quot;
  version          = var.cert_manager_chart_version
  namespace        = var.cert_manager_namespace
  create_namespace = true

  set {
    name  = &quot;installCRDs&quot;
    value = true
  }

}
</code></pre>
<p>Reference to custom chart:</p>
<pre><code>#
# cert-issuer.tf
#
# Cert Issuer using Helm
resource &quot;helm_release&quot; &quot;cert_issuer&quot; {
  name       = &quot;cert-issuer&quot;
  repository = path.module
  chart      = &quot;cert-issuer&quot;
  namespace  = var.namespace

  set {
    name  = &quot;fullnameOverride&quot;
    value = local.issuer_name
  }

  set {
    name  = &quot;privateKeySecretRef&quot;
    value = local.issuer_name
  }

  set {
    name  = &quot;ingressClass&quot;
    value = var.ingress_class
  }

  set {
    name  = &quot;acmeEmail&quot;
    value = var.cert_manager_email
  }

  set {
    name  = &quot;acmeServer&quot;
    value = var.acme_server
  }

  depends_on = [helm_release.cert_manager]
}
</code></pre>
<p>You can see that the above use of <code>helm_release</code> is referencing itself locally as the repository, which requires you to have a custom helm chart, like this:</p>
<pre><code># ./cluster-issuer/cluster-issuer.yaml

apiVersion: cert-manager.io/v1alpha2
kind: ClusterIssuer
metadata:
 name: {{ include &quot;cert-issuer.fullname&quot; . }}
 namespace: {{ .Release.Namespace }}
spec:
 acme:
   # The ACME server URL
   server: {{ .Values.acmeServer }}
   email: {{ .Values.acmeEmail }}
   # Name of a secret used to store the ACME account private key
   privateKeySecretRef:
     name: {{ .Values.privateKeySecretRef }}
   # Enable the HTTP-01 challenge provider
   solvers:
   - http01:
       ingress:
         class: {{ .Values.ingressClass }}
</code></pre>
<p>For some reason, this avoids the dependency check terraform uses to throw the error and works fine to get this installed in a single <code>apply</code></p>
<p>This could be further simplified by not using values.yaml values by creating a pure chart.</p>
<p>** Note, I think another work around is one can use a provisioner like 'local-exec' or 'remote-exec' after a cluser is created to run the kubectl apply command for the CRds directly, but I haven't tested this yet. It would also still require that your provisioning environment have kubectl installed and .kubeconfig properly configured, creating a dependency tree.</p>
<p>Also, that is of course not fully working code. for a full example of the module to use or fork, see <a href="https://github.com/DeimosCloud/terraform-kubernetes-cert-manager" rel="nofollow noreferrer">this github repo</a>.</p>
