简体   繁体   中英

How to call Config block multiple times in terraform cloud search resource

Hi I'm trying to add a dynamic block in the cloud search index_field config block but no use getting a lot of errors

        resource "aws_cloudsearch_domain" "this" {
  name = "${var.name}"

  scaling_parameters {
    desired_instance_type = "${var.instance_type}"
  }

  dynamic "index_field" {
    for_each = var.index_field

    content {
      name            = index_field.value["name"]
      type            = index_field.value["type"]
      search          = index_field.value["search"]
      return          = index_field.value["return"]
      sort            = index_field.value["sort"]
      highlight       = index_field.value["highlight"]
      analysis_scheme = index_field.value["analysis_scheme"]

    }  

  }

}

main.tf file

    module "cloudsearch" {
    source = "./modules"
      name = "demo"
      instance_type = "search.2xlarge"

      index_field = {
        name = "hello"
        
        type = "text"
        index_name = "hello"
        search = "true"
        return = false
        sort = false
        highlight = true
        analysis_scheme = "_en_default_"
        }

}

var.tf file -

variable "name" {
    type = string
    description = "value"
}

variable "instance_type" {
  type = string
  description = "value"
}


variable "index_field" {
  description = ""
  type        = any
  default     = null
}

I'm getting below error

│ Error: Invalid index │ │ on modules/main.tf line 18, in resource "aws_cloudsearch_domain" "this": │ 18: analysis_scheme = index_field.value["analysis_scheme"] │ ├──────────────── │ │ index_field.value is "text" │ │ This value does not have any indices.

help would be appreciated, thanks

The issue here is that you are using the key of index_field variable but the value is the whole block. So you need another key which will act as a placeholder key so the values related to that key can be accessed by the names of their keys. In other words, the way the variable is currently interpreted is:

key -> index_field
value -> {
        name = "hello"
        
        type = "text"
        index_name = "hello"
        search = "true"
        return = false
        sort = false
        highlight = true
        analysis_scheme = "_en_default_"
        }

In order for this to work the way you have envisioned it, there needs to be a slight modification to the way you are passing the variable to the child module:

module "cloudsearch" {
  source = "./modules"
  
  name = "demo"
  instance_type = "search.2xlarge"

  index_field = {

    first_index = { # <---- this has changed

      name = "hello"

      type            = "text"
      index_name      = "hello"
      search          = "true"
      return          = false
      sort            = false
      highlight       = true
      analysis_scheme = "_en_default_"
    }
  }

}

Note that you can also use a different syntax when getting the values from the variable:

resource "aws_cloudsearch_domain" "this" {
  name = "test"

  scaling_parameters {
    desired_instance_type = "search.2xlarge"
  }

  dynamic "index_field" {
    for_each = local.index_field

    content {
      name            = index_field.value.name
      type            = index_field.value.type
      search          = index_field.value.search
      return          = index_field.value.return
      sort            = index_field.value.sort
      highlight       = index_field.value.highlight
      analysis_scheme = index_field.value.analysis_scheme

    }

  }

}

The plan shows this:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudsearch_domain.this will be created
  + resource "aws_cloudsearch_domain" "this" {
      + arn                       = (known after apply)
      + document_service_endpoint = (known after apply)
      + domain_id                 = (known after apply)
      + id                        = (known after apply)
      + multi_az                  = (known after apply)
      + name                      = "test"
      + search_service_endpoint   = (known after apply)

      + endpoint_options {
          + enforce_https       = (known after apply)
          + tls_security_policy = (known after apply)
        }

      + index_field {
          + analysis_scheme = "_en_default_"
          + highlight       = true
          + name            = "hello"
          + return          = false
          + search          = true
          + sort            = false
          + type            = "text"
        }

      + scaling_parameters {
          + desired_instance_type     = "search.2xlarge"
          + desired_partition_count   = (known after apply)
          + desired_replication_count = (known after apply)
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM