I am using this kubectl_manifest (Terraform Registry) resource and within the YAML definition inside the resource, I need to pass few variables thats referenced from variables.tf file (depending on the environment we deploy).
SO in here env is a variable in variables.tf and thats being consumed in other resources and it needs to be in here in this kubectl_manifest yaml_body too.
Is there a way I can parameterize yaml body so that I can pass some variables into it?
I was reading about yamlencode and yamldecode, but dont see the way of passing it from variables.tf file.
The general principle of using yamlencode here would be something like this:
resource "kubectl_manifest" "my-configmap" {
# ...
yaml_body = yamlencode({
apiVersion = "v1"
kind = "ConfigMap"
metadata = {
labels = {
"app.kubernetes.io/component" = "application"
}
name = "my-configmap"
}
data = {
"test.yaml" = yamlencode({
# (skipping over some stuff that's quite similar to
# what I already illustrated above...)
spec = {
allowConcurrency = true
generate = {
command = [
"apps.sh",
"generate",
".",
"-s",
"${var.environment}:deployaction",
]
}
}
})
}
})
# ...
}
If I’m reading your YAML correctly it seems like this is a YAML document with another YAML document embedded inside it as a nested string, and so I used a nested yamlencode to achieve that. If I misunderstood what your example was intending to do then that particular decision might not make sense.
But that detail notwithstanding, the general idea here is that instead of thinking about writing YAML yourself, you would instead write a Terraform expression that produces a data structure that Terraform can then encode into YAML for you. Doing this means that you can then use any of Terraform’s normal expression types to construct that data structure.
In this particular case, I wrote "${var.environment}:deployaction" as part of the command list, which means that Terraform will expect to find a variable "environment" declaration elsewhere in the module, and will interpolate whatever value that variable is assigned when calling the module.
Hi @apparentlymart , since we were on this topic previously, I want to pick your thought process on the same.
SO instead of using kubectl_manifest, I see kubernetes_config_map_v1 resource and that suits your reply of rather than handing over YAML writing Terraform expression will be the right way.
If I use the kubernetes_config_map_v1 resource, I see the data section is where the plugin needs to go and I dont see enough documentation on adding my plugin into the kubernetes_config_map_v1 resource.
Any thoughts on how to add the “data” section here?