Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass variables to a yaml file in heml.tf?

I have a file for creating terraform resources with helm helm.tf.

In this file I create a honeycomb agent and need to pass in some watchers, so I'm using a yaml file for configuration. Here is the snippet from helm.tf:

resource "helm_release" "honeycomb" {
  version = "0.11.0"
  depends_on = [module.eks]
  repository = "https://honeycombio.github.io/helm-charts"
  chart = "honeycomb"
  name = "honeycomb"

  values = [
    file("modules/kubernetes/helm/honeycomb.yml")
  ]
}

and here is the yaml file

agent:
  watchers:
    - labelSelector: "app=my-app"
      namespace: my-namespace
      dataset: {{$env}}
      parser:
        name: nginx
        dataset: {{$env}}
        options:
          log_format: "blah"

Unfortunately my attempt at setting the variables with {{$x}} has not worked, so how would I send the env variable to the yaml values file? I have the variable available to me in the tf file but am unsure how to set it up in the values file.

Thanks

like image 788
Kathy Rindhoops Avatar asked Nov 05 '20 11:11

Kathy Rindhoops


People also ask

How do I pass a variable in YAML file?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.

Can you use variables in YAML files?

YAML does not natively support variable placeholders. Anchors and Aliases almost provide the desired functionality, but these do not work as variable placeholders that can be inserted into arbitrary regions throughout the YAML text. They must be placed as separate YAML nodes.

Can YAML use environment variables?

PY-yaml library doesn't resolve environment variables by default. You need to define an implicit resolver that will find the regex that defines an environment variable and execute a function to resolve it. You can do it through yaml.

What is {{ }} In YAML?

If a value after a colon starts with a “{”, YAML will think it is a dictionary, so you must quote it, like so: foo: "{{ variable }}" If your value starts with a quote the entire value must be quoted, not just part of it.


1 Answers

You may use templatefile function

main.tf

resource "helm_release" "honeycomb" {
  version    = "0.11.0"
  depends_on = [module.eks]
  repository = "https://honeycombio.github.io/helm-charts"
  chart      = "honeycomb"
  name       = "honeycomb"

  values = [
    templatefile("modules/kubernetes/helm/honeycomb.yml", { env = "${var.env}" })
  ]
}

honeycomb.yml

agent:
  watchers:
    - labelSelector: "app=my-app"
      namespace: my-namespace
      dataset: "${env}"
      parser:
        name: nginx
        dataset: "${env}"
        options:
          log_format: "blah"
like image 60
rajesh-nitc Avatar answered Sep 21 '22 18:09

rajesh-nitc