如何在变量中使用地形数据

How can I use terraform data in variables

我有如下脚本

variables.tf


data "google_secret_manager_secret_version" "my_secret" {
  secret = "my-secret"
}

variable "clients" {
  description = "clients data"
  default = [
    {
      id           = { stringValue = "test" }
      clientSecret = { stringValue = "${data.google_secret_manager_secret_version.my_secret.secret_data}" }
      eventType    = { stringValue = "something" }
    }
  ]
}

main.tf

resource "google_firestore_document" "clients" {
  for_each = {
    for index, client in var.clients :
    index => client
  }
  project     = var.project_id
  collection  = "clients"
  document_id = each.value.id.stringValue
  fields      = jsonencode(each.value)
}

但是当我 运行 它时我得到这个错误

Error: Variables not allowed
  on variables.tf line 16, in variable "clients":
  16:       clientSecret        = { stringValue = "${data.google_secret_manager_secret_version.my_secret.secret_data}" }

Variables may not be used here.

想知道如何使用从秘密管理器读取的动态值并将其用于变量或任何其他解决方案

我终于这样了

variables.tf

variable "clients" {
  description = "clients data"
  default = {
    X = {
      id                  = { stringValue = "test" }
      eventType           = { stringValue = "something" }
    }
  }
}

main.tf

data "google_secret_manager_secret_version" "my_secret" {
  project = var.project_id
  secret  = "my-secret"
}

resource "google_firestore_document" "clients-x" {
  project     = var.project_id
  collection  = "clients"
  document_id = var.clients.X.id.stringValue
  fields = jsonencode(merge(
    var.clients.X,
    {
      clientSecret = { stringValue = data.google_secret_manager_secret_version.my_secret.secret_data }
    }
  ))
}