user16958659
user16958659

Reputation:

How to multi line string in terraform

I am trying below from an example

resource "google_logging_metric" "my_metric" { 
  description = "Check for logs of some cron job\t" 
  name        = "mycj-logs" 
  filter      = "resource.type=\"k8s_container\" AND resource.labels.cluster_name=\"${local.k8s_name}\" AND resource.labels.namespace_name=\"workable\" AND resource.labels.container_name=\"mycontainer-cronjob\" \nresource.labels.pod_name:\"my-pod\"" 
  project     = "${data.terraform_remote_state.gke_k8s_env.project_id}" 
 
  metric_descriptor { 
    metric_kind = "DELTA" 
    value_type  = "INT64" 
  } 
}

Is there a way to make the filter field multiline?

Upvotes: 10

Views: 25757

Answers (2)

Kim
Kim

Reputation: 1684

If it's a matter of formatting the output, this answer covers it.

If you want to make your code more readable, i.e. split the long string into multiple clauses with a common separator, I've found join() useful:

resource "google_logging_metric" "my_metric" { 
  description = "Check for logs of some cron job\t" 
  name        = "mycj-logs" 
  project     = "${data.terraform_remote_state.gke_k8s_env.project_id}" 
  filter = join(" AND ",
    [
        "resource.type=\"k8s_container\"",
        "resource.labels.cluster_name=\"${local.k8s_name}\"",
        "resource.labels.namespace_name=\"workable\"",
        "resource.labels.container_name=\"mycontainer-cronjob\"",
        "resource.labels.pod_name:\"my-pod\""
    ]
  ) 
  metric_descriptor { 
    metric_kind = "DELTA" 
    value_type  = "INT64" 
  } 
}

Note that setting out the code this way shows that in the OP there may (or may not) be a missing AND in the filter expression, between the last and second last clauses.

Because the original expression is one long line, this is very hard to see and slow to read/maintain. With the join expression the intent is clearer I think.

Upvotes: 13

Alberto S.
Alberto S.

Reputation: 7649

A quick search in Google sends you to the documentation: https://www.terraform.io/docs/language/expressions/strings.html#heredoc-strings

You just need to write something like

<<EOT
hello
world
EOT

Upvotes: 9

Related Questions