You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform的SignalFx查询中正确引用字符串集合变量

问题描述

我定义了如下Terraform变量:

variable "operations" {
  type = set(string)
  default = [
    "create",
    "setFavourite",
    "planNoSce"
  ]
}

我尝试在以下SignalFx查询中引用该变量:

resource "signalfx_time_chart" "operations_errors" {

  name = "Operations Errors"

  program_text = <<-EOF
A = data('some.count',
filter=(not filter('status', 'success'))
and filter('operation', ${var.operations}), rollup='sum').sum(by['operation']).publish(label='Errors')
EOF
}

但我希望集合中的每个元素以带单引号的字符串形式传入,使查询语句变为:

... and filter('operation', 'create', 'setFavourite', 'planNoSce'), ...

请问该如何实现?

解决方案

结合Terraform的formatlist()和join()函数即可实现需求,修改后的代码如下:

resource "signalfx_time_chart" "operations_errors" {
  name = "Operations Errors"

  program_text = <<-EOF
A = data('some.count',
filter=(not filter('status', 'success'))
and filter('operation', ${join(", ", formatlist("'%s'", var.operations))}), rollup='sum').sum(by['operation']).publish(label='Errors')
EOF
}

逻辑说明:

  • formatlist("'%s'", var.operations):遍历var.operations集合的每个元素,用'%s'格式将每个字符串包裹上单引号,得到带单引号的元素列表。
  • join(", ", ...):把处理后的列表元素用「逗号+空格」连接成一个完整字符串,最终输出'create', 'setFavourite', 'planNoSce',完全匹配SignalFx查询的语法要求。

内容的提问来源于stack exchange,提问作者S.Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 04:22:03