terraform-provider-elasticstack icon indicating copy to clipboard operation
terraform-provider-elasticstack copied to clipboard

[Bug] elasticstack_kibana_data_view creates resource, but fails to track this in state

Open jonnydh opened this issue 9 months ago • 1 comments

Describe the bug

TL;DR: Terraform reports that it fails to create the resource (so is not tracked in state), but a subsequent terraform run reports that the resource cannot be created, as a duplicate resource already exists.

When applying terraform to create multipleelasticstack_kibana_data_view resources from scratch, we regularly see the following error in our first run

│ Error: Unable to create data view
--
4912 | │
4913 | │   with elasticstack_kibana_data_view.index_pattern["match_all_logstash_ingested_nginx_logs_kibana_index_pattern"],
4914 | │   on main.tf line 20, in resource "elasticstack_kibana_data_view" "index_pattern":
4915 | │   20: resource "elasticstack_kibana_data_view" "index_pattern" {
4916 | │
4917 | │ Failed with: {"statusCode":400,"error":"Bad
4918 | │ Request","message":"[config:8.11.1]: version conflict, required seqNo [11],
4919 | │ primary term [1]. current document has seqNo [12] and primary term [1]:
4920 | │ version_conflict_engine_exception\n\tRoot
4921 | │ causes:\n\t\tversion_conflict_engine_exception: [config:8.11.1]: version
4922 | │ conflict, required seqNo [11], primary term [1]. current document has seqNo
4923 | │ [12] and primary term [1]"}

This typically happens for 3-5 data views out of 15. However when we run the terraform apply again, the resources that 'failed' to be created on the first run, cannot be created as they already exist:

328 | │
329 | │   with elasticstack_kibana_data_view.index_pattern["match_all_logstash_ingested_nginx_logs_kibana_index_pattern"],
330 | │   on main.tf line 20, in resource "elasticstack_kibana_data_view" "index_pattern":
331 | │   20: resource "elasticstack_kibana_data_view" "index_pattern" {
332 | │
333 | │ Failed with: {"statusCode":400,"error":"Bad Request","message":"Duplicate
334 | │ data view: logstash-nginx*"}
335 | ╵

To Reproduce Steps to reproduce the behavior:

  1. We run our Terraform pipeline in AWS CodeBuild

  2. Worth noting that we see this in our lab environments, which tear down our entire logging stack and then rebuild it each week. The 'component' we create before Kibana is ElasticSearch. (Adding this as context as to why we might be seeing the first error).

  3. TF configuration used:

resource "elasticstack_kibana_data_view" "index_pattern" {
  for_each = var.index_patterns

  data_view = {
    allow_no_index  = true
    id              = each.key
    name            = each.value.title
    time_field_name = each.value.time_field
    title           = each.value.title
  }
}
variable "index_patterns" {
  description = "Map of index patterns"
  type = map(object({
    title      = string
    time_field = string
  }))
  default = {
    "match_all_logstash_ingested_nginx_logs_kibana_index_pattern" = {
      time_field = "@timestamp"
      title      = "logstash-nginx*"
    }
  }
}
  1. TF operations to execute to get the error: terraform apply

Expected behavior We would expect that either:

  • The resource should not be created, if the provider reports a failure.
  • The resource should be created, if this error message isn't relevant to the resource creation.

Versions (please complete the following information):

  • OS: [Linux]
  • Terraform Version [1.5.5]
  • Provider version [0.11.0]
  • Elasticsearch Version [8.11.1]

jonnydh avatar May 02 '24 11:05 jonnydh