使Terraform资源键变成多行

28

我在Terraform中声明了一个google_logging_metric资源(使用版本0.11.14)。

我有以下声明:

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"
  }
}

有没有办法使filter字段变成多行?

local变量"${local.k8s_name}的存在使得这有点具有挑战性。


1
https://www.terraform.io/docs/configuration-0-11/variables.html#strings - Liam
1
我通过谷歌搜索“terraform多行字符串”找到了这个答案。 - Liam
我的问题的一部分是这个部分 =\"${local.k8s_name}\" 是否被适当地解析。 - pkaramol
2个回答

34
文档中得知:

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
}

EOF或EOH不起作用。当我尝试在多行上设置member = <<EOF ... EOF时,它会出错。请坚持使用普通的双引号字符串格式。 - Aseem

3
这在2022年中期已经发生了变化。 heredoc语法已经改变为:
<<EOT/<<-EOT 
... 
EOT

根据Hashicorp文档: https://developer.hashicorp.com/terraform/language/expressions/strings ^^^ 查找 'heredoc strings'
前面答案的示例现在应该是:
variable "long_key" {
  type = string
  default = <<EOT
This is a long key.
Running over several lines.
EOT
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接