如何在 Terraform 模板中转义 Logstash sprintf 表达式?

How to escape a Logstash sprintf expression in a Terraform template?

我正在尝试使用 Terraform 模板文件对使用 sprintf 表达式的 logstash 配置进行模板化。

示例:

filter {
  json { source => "message" }
  geoip {
    source => "[user][rparam][ip]"
  }
  useragent {
    source => "[user][rparam][ua]"
  }
  date {
    match => [ "[@metadata][kafka][timestamp]", "UNIX_MS" ]
  }
  mutate { add_field => { "[@metadata][id]" => "%{[id]}" } }
  mutate { add_field => { "[@metadata][eid]" => "%{[eid]}" } }
  mutate { remove_field => [ "message", "id", "eid", "@version", "tags" ] }
  if "[user]" {
    mutate { remove_field => [ "[user][rparam][ip]" ] }
  }
}

不幸的是,使用的百分比字符“%”也是 Terraform 模板语言的一部分,因此 Terraform returns 向我显示以下错误消息:

* data.template_file.logstash-output: data.template_file.logstash-output: failed to render : <template_file>:6,24-25: Invalid character; This character is not used within the language., and 1 other diagnostic(s)

如何正确转义? Terraform 文档没有留下任何线索,只指定了如何转义变量插值表达式 ($)

百分号可以用双倍的%%进行转义。上面的模板文件可以这样写,结果中Logstash sprintf表达式会被正确处理:

filter {
  json { source => "message" }
  geoip {
    source => "[user][rparam][ip]"
  }
  useragent {
    source => "[user][rparam][ua]"
  }
  date {
    match => [ "[@metadata][kafka][timestamp]", "UNIX_MS" ]
  }
  mutate { add_field => { "[@metadata][id]" => "%%{[id]}" } }
  mutate { add_field => { "[@metadata][eid]" => "%%{[eid]}" } }
  mutate { remove_field => [ "message", "id", "eid", "@version", "tags" ] }
  if "[user]" {
    mutate { remove_field => [ "[user][rparam][ip]" ] }
  }
}