phillbaker / terraform-provider-elasticsearch

An elasticsearch provider for terraform
https://registry.terraform.io/providers/phillbaker/elasticsearch
Mozilla Public License 2.0
304 stars 134 forks source link

Datetime named index recreated with each terraform apply #307

Closed mzamfirmade closed 2 years ago

mzamfirmade commented 2 years ago

Hi,

I am having an issue with the indices I create via terraform, more specific, the indices get recreated each time I apply the terraform and I am not sure that it is intended to work that way.

This is how I am setting them up

resource "elasticsearch_index" "index" {
  for_each = toset(local.indices)

  name               = "${each.value}-${formatdate("YYYY.MM.DD-000001", timestamp())}"
  number_of_shards   = 1
  number_of_replicas = 1
  aliases            = jsonencode({"${each.value}" = {}})

  depends_on = [
    elasticsearch_index_template.index_template,
    elasticsearch_opensearch_ism_policy.ism_policy,
  ]
}

This works well on the initial run but doesn't fit its purpose afterwards. Here's the output of the terraform run:

Terraform will perform the following actions:

  module.monitoring.elasticsearch_index.index["metrics"] must be replaced
-/+ resource "elasticsearch_index" "index" {
      ~ id                 = "metrics-2022.08.04-000001" -> (known after apply)
      ~ name               = "metrics-2022.08.04-000001" -> (known after apply) # forces replacement
      + rollover_alias     = (known after apply)
        # (4 unchanged attributes hidden)
    }

I'm looking for a way to avoid this, I tried adding a condition to only run this once, but that's not how terraform works really and if I try to skip the resource, terraform will see it as missing and remove the index. I also had a read through https://github.com/phillbaker/terraform-provider-elasticsearch/issues/55 since it looks fairly similar, yet doesn't seem to apply in this scenario.

Any help is greatly appreciated.

mzamfirmade commented 2 years ago

I accidentally found the issue here after the index was rolled over I updated my terraform to

resource "elasticsearch_index" "index" {
  for_each = toset(local.indices)

  name               = "<${each.value}-{now/d}-000001>"
  number_of_shards   = 1
  number_of_replicas = 1
  aliases            = jsonencode({"${each.value}" = {}})
  default_pipeline   = "clean_metrics"

  depends_on = [
    elasticsearch_index_template.index_template,
    elasticsearch_opensearch_ism_policy.ism_policy,
  ]
}

Now it looks like the index is not re-created at each apply anymore.