Terraform - Creating resources from Kubernetes YAML files containing multiple configurations

I recently decided to try and set up my own kubernetes cluster using terraform. One of the challenges I found was using YAML files containing several configurations separated by "---". The providers/resources I could find all required the format to be in HCL and not YAML. Furthermore the built-in function yamldecode could not handle multiple configurations ("---") in the same file. The configuration I wanted to install was argocd (containing 50 or so configurations). Alternatively I could have converted the yaml configurations to HCL manually and split them into files, but I decided to go with a more automatic solution. Below is what I came up with:

provider "kubernetes" {
  config_path    = "${path.module}/.kube/config"
  config_context = "<config_context_name>"
}

locals {
  raw_argocd_manifests = 
      split("---", file("${path.module}/manifests/argocd/install.yaml"))
  hcl_argocd_manifests = 
      [for argo_manifest in local.raw_argocd_manifests : yamldecode(argo_manifest)]
}

resource "kubernetes_manifest" "argocd-manifests" {
  count = length(local.hcl_argocd_manifests)
  manifest = local.hcl_argocd_manifests[count.index]
}

In the above I use locals in order to create two variables. The first (raw_argocd_manifests) reads the file located at "/manifests/argocd/install.yaml" and splits the contents at "---". The second (hcl_argocd_manifests) takes the individual configurations and transforms them into HCL using yamldecode. Next I use the kubernetes_manifest resource in order to create the configurations using count.index.

Disclaimer: I am rather new to Terraform, I am not sure if the above is the best way to go about this, but it gets the job done!

I hope this helps someone out there :)