Home > database >  Terraform helm_release fails on set labels
Terraform helm_release fails on set labels

Time:05-17

I wish to set labels inside a Helm chart using the Terraform helm_release resource with a helpers function.

When using the chart values there are no issues, as seen here:

values.yaml

mylabels:
  name: "foo"
  type: "boo"

_helpers.tpl

{{- define "aws.labels" -}}
{{- range $k, $v := .Values.mylabels }}
{{ $k }}: {{ $v | quote }}
{{- end -}}
{{- end -}}

deployment.yaml

metadata:
  labels:
  {{- include "aws.labels" . | trim | nindent 4 }}

But when values are passed in from the helm_release resource it fails.

  set {
    name  = "mylabels"
    value = yamlencode(var.aws_tags) 
  }

The output values for the above is:

    set {
        name  = "mylabels"
        value = <<-EOT
            "Environment": "123"
            "Owner": "xyz"
        EOT
    }

Which generates error:

│ Error: template: aws-efs-csi-driver/templates/controller-deployment.yaml:9:6: executing "aws-efs-csi-driver/templates/controller-deployment.yaml" at <include "aws.tags" .>: error calling include: template: aws-efs-csi-driver/templates/_helpers.tpl:68:27: executing "aws.tags" at <.Values.mylabels>: range can't iterate over 
│ "Environment": "123"
│ "Owner": "xyz"

Any ideas or pointers would be greatly appreciated :-)

Solution

Thank you @jordanm that worked :~)

CodePudding user response:

When using set in this way, it's the same as --set on the helm cli. You can't provide a yaml value that includes multiple values and must set each individually.

  set {
    name  = "mylabels.Environment"
    value = var.aws_tags["Environment"] 
  }
  set {
    name  = "mylabels.Owner"
    value =  var.aws_tags["Owner"]
  }

You may also be able the dynamic feature of terraform to iterate over your aws_tags variable:

dynamic "set" {
  for_each = var.aws_tags
  
  content {
    name = "mylabels.${set.key}"
    value = set.value
  }
}

CodePudding user response:

Thank you @jordanm your ideas worked :~)

  • Related