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 behavior when trying to delete an index via Terraform #85

Closed jlustin closed 1 month ago

jlustin commented 1 year ago

What is the bug?

Hi, I am currently looking at ways to delete an index and the behavior I am seeing when modifying a opensearch_index resource is very confusing.

  1. When modifying aliases of an existing index, the index gets destroyed then recreated by Terraform, logs saying that the index must be replaced, in which case the index is destroyed and recreated. However, when modifying other attributes such as number_of_replicas or refresh_interval, the index gets updated in place and the index is kept.
  2. When setting force_destroy to true or flicking it from false to true, Terraform indicates in the plan that the index is changed, but does not actually destroy the index.
  3. When simply removing the index module from the repository in an attempt to delete it, I get an error saying that Provider configuration not present. This also happens whether I delete the targeted index manually from the OS dashboard or not; so if I delete the index manually and do not remove the module, the index will get recreated, and if I remove the module, I get that error. I am able to get through this stage by doing a state rm on the module, but this does not actually delete the index either.

How can one reproduce the bug?

Create an index without aliases nor force_destroy:

  1. Set aliases for the index. The index will get destroyed and recreated.
  2. Set force_destroy to true. Nothing happens to the index.
  3. Remove the index module. Plan fails.

What is the expected behavior?

  1. Aliases for given index are updated but index is not recreated. Right now, since the index is recreated, all documents are lost.
  2. Index is destroyed whether it contains documents or not.
  3. I would expect the index to be deleted by Terraform if its module is removed from the Terraform repository, which is what I am trying to achieve.

What is your host/environment?

Terraform v1.3.5
on linux_amd64
...
+ provider registry.terraform.io/opensearch-project/opensearch v1.0.0

Do you have any additional context?

Context = I am seeing this behavior when trying to delete an index. What would be the correct way to achieve deleting an index that was created using the terraform provider? Is this supported somehow?

prudhvigodithi commented 1 year ago

Hey @jlustin I assume you are using the latest version of the provider https://registry.terraform.io/providers/opensearch-project/opensearch/2.0.0 In this comment I have tested creating index and index template and able to create and destroy, please check https://github.com/opensearch-project/terraform-provider-opensearch/issues/72#issuecomment-1714363935 Adding @rblcoder @afrodidact @premkirank Thank you

premkirank commented 1 year ago

@prudhvigodithi I would like to work on this

prudhvigodithi commented 1 year ago

Thanks assigned to your @premkirank.

premkirank commented 11 months ago

I am starting on this issue from now. Had been working on some minor bugs etc to get a hang of the repo. Sorry for the delay

premkirank commented 11 months ago

@jlustin I have been able to follow along until step 2 and been able to verify what you have said. I am not able to understand step 3 i.e deleting an index module. How can I delete an index module, is there some API for this?

prudhvigodithi commented 11 months ago

Hey @jlustin as per the discussion here https://github.com/opensearch-project/terraform-provider-opensearch/issues/83#issuecomment-1787722531, the index should not get deleted when configured an alias using the provider? Can you please confirm if you are using the latest version? Thanks

jlustin commented 11 months ago

@premkirank Thanks for the update. For step 3, I just meant deleting the config of the index from the files

jlustin commented 11 months ago

@prudhvigodithi This was initially produced when using registry.terraform.io/opensearch-project/opensearch v1.0.0 as specified above; I will try once more after doing an update

jlustin commented 11 months ago

Hi both, I'm using v2.1.0 now and seeing the same behavior as initially described.

@prudhvigodithi This is the output when I try modifying the alias of an index via the provider; the index would get replaced: image

premkirank commented 11 months ago

@premkirank Thanks for the update. For step 3, I just meant deleting the config of the index from the files

@jlustin could you just provide a sample for this file after deleting the config. It will help make it clear.

imanis commented 5 months ago

HI @premkirank : can you please start by the first step (Set aliases for the index. The index will get destroyed and recreated.) Is that a normal behavior ? Adding an alias to an index should not force recreating the index.

anuborah commented 1 month ago

HI @premkirank @prudhvigodithi Is there any update on the above bug when it will be fixed? Adding an alias to the existing index the index gets deleted :(

prudhvigodithi commented 1 month ago

Hey @anuborah thanks for sharing your thoughts, I agree this bug has to be fixed, we can looking for some contributors, is it possible for you to fix this bug by creating a PR? I'm happy to help and troubleshoot here. We can talk more in this slack channel as well https://opensearch.slack.com/archives/C051ZNQ16KG. Thank you Adding @imanis @jlustin @premkirank @rblcoder @getsaurabh02

prudhvigodithi commented 1 month ago

I have a PR open for this bug https://github.com/opensearch-project/terraform-provider-opensearch/pull/212, we can now update the aliases section on existing opensearch_index without destroying or force update.

  aliases = jsonencode(
   {
    "log": {
      "is_write_index": true
    }
  }
  )

Example output when the index is created with no aliases


curl http://localhost:9200/sample/_alias  -u 'admin:myStrongPassword123@456' | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    25  100    25    0     0   8389      0 --:--:-- --:--:-- --:--:-- 12500
{
  "sample": {
    "aliases": {}
  }
}

Example terraform apply output when the index is created and aliases is added

# opensearch_index.index will be updated in-place
  ~ resource "opensearch_index" "index" {
      + aliases            = jsonencode(
            {
              + log = {
                  + is_write_index = true
                }
            }
        )
        id                 = "sample"
        name               = "sample"
        # (4 unchanged attributes hidden)
    }

curl http://localhost:9200/sample/_alias  -u 'admin:myStrongPassword123@456' | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    54  100    54    0     0  19244      0 --:--:-- --:--:-- --:--:-- 27000
{
  "sample": {
    "aliases": {
      "log": {
        "is_write_index": true
      }
    }
  }
}

Thanks @getsaurabh02

prudhvigodithi commented 1 month ago

Hey @jlustin the index cannot be deleted if the documents are present thats the behavior of the OpenSearch and nothing to do with the terraform provider.

prudhvigodithi commented 1 month ago

The terraform provider 2.3.1 is released with opensearch_index aliases bug fix, @anuborah @imanis @jlustin please take a look. Thanks

prudhvigodithi commented 1 month ago

Closing this issue since the terraform provider 2.3.1 is released with opensearch_index aliases bug fix, please feel free to add a comment or re-open if required. Thank you @getsaurabh02