Last active
August 24, 2023 11:12
-
-
Save imranismail/6246c32d38888c77b8becd0b04587970 to your computer and use it in GitHub Desktop.
Using Kustomize with Terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: myapp | |
spec: | |
selector: | |
matchLabels: | |
app: myapp | |
template: | |
metadata: | |
labels: | |
app: myapp | |
spec: | |
containers: | |
- name: myapp | |
image: nginx | |
resources: | |
limits: | |
memory: "128Mi" | |
cpu: "500m" | |
ports: | |
- containerPort: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: kustomize.config.k8s.io/v1beta1 | |
kind: Kustomization | |
resources: | |
- deployment.yaml | |
- service.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "terraform_data" "kustomize" { | |
triggers_replace = timestamp() | |
provisioner "local-exec" { | |
command = "mkdir -p ${path.module}/.terraform/tmp" | |
} | |
provisioner "local-exec" { | |
command = "kustomize build ${path.module} > ${path.module}/.terraform/tmp/manifests.yaml" | |
} | |
} | |
data "local_file" "manifests" { | |
depends_on = [terraform_data.kustomize] | |
filename = "${path.module}/.terraform/tmp/manifests.yaml" | |
} | |
locals { | |
manifests = { | |
for manifest in [ | |
for doc in split( | |
"---", data.local_file.manifests.content | |
) : | |
yamldecode(doc) | |
if doc != "" | |
] : | |
join("/", [for part in [manifest.apiVersion, manifest.kind, try(manifest.metadata.namespace, ""), manifest.metadata.name] : part if part != ""]) => manifest | |
} | |
} | |
resource "kubernetes_manifest" "this" { | |
for_each = local.manifests | |
manifest = each.value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: myapp | |
spec: | |
selector: | |
app: myapp | |
ports: | |
- port: 8080 | |
targetPort: 8080 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an attempt to use Kustomize with Terraform along with dealing with a yaml stream that is generated by Kustomize.
Tested with
terraform = "~> 1.5"
The
kubernetes_manifest
resource name will be according to K8S standard ofkubernetes_manifest.this["apps/v1/Deployment/envoy/envoy-control-plane"]