1

I am using terraform resource google_firestore_document to create firestor document db and i was abel to create it successfully. I am trying to convert it to a module for which I need to build the values in jsonencode format. example:

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

I have created a variable as below.

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

and main.tf is:

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

output is and it is not correct.

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

I tried to build with fields = jsonencode({var.fields}) but it says Expected an equals sign ("=") to mark the beginning of the attribute value Please let me know how can we achieve it without two fields section.

1 Answers1

0

I think you should use = instead of : in variable definition. I found example in this post. I think it will be something like:

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

Although I do not have playground to test it (so please treat it as pseudo code).

vitooh
  • 4,132
  • 1
  • 5
  • 16