Skip to content

Instantly share code, notes, and snippets.

@CRThaze
Created November 9, 2025 18:59
Show Gist options
  • Save CRThaze/56650b6f30eb9f1c5ab7878484741fb2 to your computer and use it in GitHub Desktop.
Save CRThaze/56650b6f30eb9f1c5ab7878484741fb2 to your computer and use it in GitHub Desktop.
How to split and flatten the output of a helm_template data source so that you can then use these with kubernetes_manifest resources from the terraform provider, so as to not mix statemanagement.
resource "kubernetes_namespace" "metallb-system" {
metadata {
name = "metallb-system"
}
}
data "helm_template" "metallb" {
name = "metallb"
namespace = "metallb-system"
repository = "https://metallb.github.io/metallb"
chart = "metallb"
}
locals {
# Process the manifests with content-based keys
processed_manifests = {
for item in flatten([
for orig_key, value in data.helm_template.metallb.manifests : [
for idx, doc in split("\n---\n", value) : {
key = replace(
join("/", [
try(yamldecode(idx == 0 ? doc : "---\n${doc}").apiVersion, ""),
try(yamldecode(idx == 0 ? doc : "---\n${doc}").metadata.namespace, ""),
try(yamldecode(idx == 0 ? doc : "---\n${doc}").kind, ""),
try(yamldecode(idx == 0 ? doc : "---\n${doc}").metadata.name, "")
]),
"////", "/"
)
value = idx == 0 ? doc : "---\n${doc}"
} if try(yamldecode(idx == 0 ? doc : "---\n${doc}"), null) != null
]
]) : item.key => item.value
}
}
resource "kubernetes_manifest" "metallb" {
for_each = local.processed_manifests
manifest = yamldecode(each.value)
depends_on = [
kubernetes_namespace.metallb-system
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment