opensearch-project / terraform-provider-opensearch

https://registry.terraform.io/providers/opensearch-project/opensearch
Apache License 2.0
75 stars 58 forks source link

[BUG] opensearch_index_template keep doing 'update in-place' every apply #49

Closed joaoolavobv closed 1 year ago

joaoolavobv commented 1 year ago

What is the bug?

I've a opensearch_index_template resource. Applying the plan, it creates the resource correctly on my AWS OpenSearch 2.5 domain. If I run plan/apply again, it keeps saying that this resourse will be updated.

How can one reproduce the bug?

Here's a part of the terraform file:

terraform {
  required_providers {
    opensearch = {
      source = "opensearch-project/opensearch"
      version = "1.0.0"
    }
  }
}
[...]
resource "opensearch_index_template" "index_template" {
  for_each =  toset(local.apps_file)
  name = "${each.key}"
  body = jsonencode(
    {
      index_patterns = [
        "${each.key}-*"
      ]
      template = {
        mappings = {
          properties = {}
        }
        settings = {
          index = {
            number_of_shards = "1"
          }
        }
      }
    }
  )
}

I also defined the "body" part as bellow, and the behavor is the same:

  body = <<EOF
{
  "index_patterns": [
    "${each.key}-*"
  ],
  "template": {
    "settings": {
      "index":{
          "number_of_shards": "1"
      }
    },
    "mappings": {
      "properties": {}
    }
  }
}
EOF
}

After apply, the template is there:

GET _template/xyz
{
  "xyz": {
    "order": 0,
    "index_patterns": [
      "xyz-*"
    ],
    "settings": {},
    "mappings": {},
    "aliases": {}
  }
}

And if I run plan/apply again, and again, I always get:

[...]
module.opensearch.opensearch_index_template.index_template["xyz"]: Refreshing state... [id=xyz]
[...]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.opensearch.opensearch_index_template.index_template["xyz"] will be updated in-place
  ~ resource "opensearch_index_template" "index_template" {
      ~ body = jsonencode(
          ~ {
              + template       = {
                  + mappings = {
                      + properties = {}
                    }
                  + settings = {
                      + index = {
                          + number_of_shards = "1"
                        }
                    }
                }
                # (1 unchanged attribute hidden)
            }
        )
        id   = "xyz"
        name = "xyz"
    }

Plan: 0 to add, 1 to change, 0 to destroy.

What is the expected behavior?

Nothing to do after the first apply.

What is your host/environment?

Do you have any additional context?

I use the for_each loop in order to create a resource per application listed on a file. It creates correctly internal users, roles and role mappings, no issue on these.

joaoolavobv commented 1 year ago

Sorry, my mistake. Removing the template section and moving settings and mappings to upper lever (as showed bellow), it worked fine.

resource "opensearch_index_template" "index_template" {
  for_each = toset(local.apps_file)
  name = "${each.key}"
  body = jsonencode(
    {
      index_patterns = [
        "${each.key}-*"
      ]
      mappings = {
        properties = {}
      }
      settings = {
        index = {
          number_of_shards = "1"
        }
      }
    }
  )
}