How can I pass a variable into a YAML when calling via kubectl_manifest resource?

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 file (depending on the environment we deploy).

resource "kubectl_manifest" "my-configmap" {
    yaml_body = <<YAML
apiVersion: v1
kind: ConfigMap
  labels: application
  test.yaml: |
    apiVersion: v1alpha1
    kind: Deployment
      name: appserver
      allowConcurrency: true
          - generate
          - "."
          - "-s"
          - "<env>:deployaction"

SO in here env is a variable in 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 file.


Hi @yerneniv,

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 = {
          "" = "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 = [
    # ...

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.

Yup, it worked @apparentlymart . Appreciate the help here. Adding the ${var.env} worked like charm.
Appreciate it pal.

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?

Appreciate the reply here mate!!

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.