Using helm from terraform

When it comes into configuring a helm chart using terraform we can configure it's values in two different ways: By using set or by pushing a template with it's values. Let's take a look:

Maybe the more straightforward way is by simply setting the variables to it's value using a set block like follows:

resource "helm_release" "datadog" {
  name       = "datadog"
  repository = "https://helm.datadoghq.com"
  chart      = "datadog"
  version    = "2.10.1"
  timeout    = 600

  namespace = var.namespace

  set {
    name  = "datadog.site"
    value = var.datadog_site
  }
}

Even though it's a nice way for setting a handful of values, as we need to push more values the terraform code becomes messier. Pushing a values.yaml template with the variables we want to set (or transform) is just as easy as setting a value using set:

resource "helm_release" "datadog" {
  name       = "datadog"
  repository = "https://helm.datadoghq.com"
  chart      = "datadog"
  version    = "2.10.1"
  timeout    = 600

  namespace = var.namespace

  values = [
    templatefile("${path.module}/datadog.yaml", {
      DATADOG_SITE = var.datadog_site,
    })
  ]
}

The template file will have to contain the values that the helm chart expects in yaml format. We can set variable using the ${} syntax. For example:

datadog:
  apiKey: ${DATADOG_SITE}

Posted on 25/03/2021