Make Terraform resource key multiline
Asked Answered
E

2

29

I am declaring a google_logging_metric resource in Terraform (using version 0.11.14)

I have the following declaration

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?

The existence of the local variable "${local.k8s_name} makes it a bit challenging.

Electrodialysis answered 17/3, 2020 at 12:2 Comment(3)
terraform.io/docs/configuration-0-11/variables.html#stringsDissension
I literally found that by googling, terraform multiline stringDissension
part of my question is whether this section =\"${local.k8s_name}\" is parsed appropriatelyElectrodialysis
D
35

From the docs

String values are simple and represent a basic key to value mapping where the key is the variable name. An example is:

variable "key" {
  type    = "string"
  default = "value"
}

A multi-line string value can be provided using heredoc syntax.

variable "long_key" {
  type = "string"
  default = <<EOF
This is a long key.
Running over several lines.
EOF
}
Dissension answered 17/3, 2020 at 12:2 Comment(1)
EOF or EOH doesnt work . It gives error when I tried to set member = <<EOF ... EOF on multiple lines. Stick to normal double quoted string formatForego
F
5

This has changed as of mid-2022. The heredoc syntax has been changed to:

<<EOT/<<-EOT 
... 
EOT

per Hashicorp documentation: https://developer.hashicorp.com/terraform/language/expressions/strings

^^^ Find 'heredoc strings'

The previous answer's example should now be:

variable "long_key" {
  type = string
  default = <<EOT
This is a long key.
Running over several lines.
EOT
}
Frantic answered 16/8, 2023 at 22:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.