How to pass output from terraform to Helm subchart

3/1/2021

I'm using 0.14.2 Terraform version and 1.3.2 Helm provider.

I have a terraform task where I get an output and then I use in a helm chart. So far so good. In the task where I execute de helm deploy I set the var I have to use:

resource "helm_release" "kong-deploy" {
  for_each   =  local.ob

  chart      = "./helm-charts/kong"
  name       = "kong"
  namespace  = each.value
  create_namespace = true
  version    = "platform-2.10"
  timeout = 30
  values = [file("./helm-values/${local.environment}/kong/kong-${local.environment}-${each.value}.yaml")]
   set {
      name  = "WORKER_NODE"
      value = aws_eks_node_group.managed_workers[each.value].node_group_name
      type  = "string"
    }
}

The tree directory is like that and I have to use the WORKER_NODE var in the postgres subchart.

├── charts
│   └── postgres
│       ├── Chart.yaml
│       ├── templates
│       │   ├── deployment.yaml
│       │   ├── env.yaml
│       │   └── service.yaml
│       └── values.yaml
├── Chart.yaml
├── files
│   └── purgeKongService.sh
├── templates
│   ├── configmap.yaml
│   ├── deployment.yaml
│   ├── env.yaml
│   ├── ingress.yaml
│   └── service.yaml
└── values.yaml

I tried to use this var like the other charts, but with no success:

nodeSelector:
        eks.amazonaws.com/nodegroup: "{{ .Values.WORKER_NODE }}"

HOw can I pass this var to a subchart?

-- hmar
kubernetes
kubernetes-helm
terraform

1 Answer

3/5/2021

If I'm understanding this right, you want to access the parent chart value's inside the subchart. To do that you can set it as a global value or define the subchart values separately. In this case I would set it as a global value:

values.yaml:

    global:
      WORKER_NODE: <default value>

From there, in your TF code you can set it via global.WORKER_NODE to pass to both kong chart and the postgres subchart.

Also, it's best practice to use camelCase when naming chart values. So instead of WORKER_NODE, you should do workerNode.

Best Practices: https://helm.sh/docs/chart_best_practices/values/

Subcharts and Globals: https://helm.sh/docs/chart_template_guide/subcharts_and_globals/#global-chart-values

-- cdecoux
Source: StackOverflow