使用 terraform 创建 GCP Firestore 文档数据库

问题描述

我正在使用 terraform 资源 google_firestore_document 来创建 firestor 文档数据库,并且我成功地创建了它。 我正在尝试将其转换为一个模块,我需要为其构建 jsonencode 格式的值。 例子:

 ` + fields      = jsonencode(
        {
          + akey      = {
              + stringValue = "avalue"
            }
          + createdBy = {
              + stringValue = "xyz"
            }
        }
    )`

我创建了一个如下的变量。

`variable "fields" {
    description = "field details"
    default = {"fname":{"stringValue":"xyz"}}
}`

和 main.tf 是:

   ` resource "google_firestore_document" "default" {
    project     = "var.project"
    collection  = "var.collection"
    document_id = "var.document_id"
    fields = jsonencode({ fields = var.fields})
  }`

输出正确,但不正确。

  ` + fields      = jsonencode(
        {
          + fields = {
              + fname = {
                  + stringValue = "xyz"
                }
            }
        }
      )`

我尝试使用 fields = jsonencode({var.fields}) 构建,但它说 需要等号 ("=") 来标记属性值的开头 请让我知道我们如何在没有两个字段部分的情况下实现它。

解决方法

我认为您应该在变量定义中使用 = 而不是 :。我在此 post 中找到了示例。我认为它会是这样的:

`variable "fields" {
    description = "field details"
    default = {
      "fname" = {
         "stringValue" = "xyz"
      }
    }
}`

虽然我没有操场来测试它(所以请把它当作伪代码)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...