elastic / terraform-provider-elasticstack

Terraform provider for Elastic Stack
https://registry.terraform.io/providers/elastic/elasticstack/latest/docs
Apache License 2.0
164 stars 85 forks source link

[Bug] elasticstack_kibana_data_view not storing to state #720

Closed x-dean closed 3 weeks ago

x-dean commented 3 weeks ago

Describe the bug kibana_data_view gets created but not stored in state.

Screenshots

terraform version
Terraform v1.9.4
on linux_amd64
+ provider registry.terraform.io/elastic/ec v0.10.0
+ provider registry.terraform.io/elastic/elasticstack v0.11.2
+ provider registry.terraform.io/hashicorp/azuread v2.53.1
+ provider registry.terraform.io/hashicorp/azurerm v3.100.0
+ provider registry.terraform.io/hashicorp/external v2.3.3
+ provider registry.terraform.io/hashicorp/null v3.2.2
+ provider registry.terraform.io/netascode/utils v0.2.6

terraform {
  required_providers {
    elasticstack = {
      source = "elastic/elasticstack"
      version = "0.11.2"
    }
  }
}

**terraform plan**

  # module.rbac.module.kibana_data_views["prometheus"].elasticstack_kibana_data_view.data_views will be created
  + resource "elasticstack_kibana_data_view" "data_views" {
      + data_view = {
          + allow_no_index  = false
          + id              = (known after apply)
          + name            = "prom"
          + namespaces      = []
          + time_field_name = "@timestamp"
          + title           = "metrics-prometheus.remote_write*"
        }
      + id        = (known after apply)
      + override  = false
      + space_id  = "default"
    }

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

**terraform apply**

│ Error: Missing Resource State After Create
│
│   with module.rbac.module.kibana_data_views["prometheus"].elasticstack_kibana_data_view.data_views,
│   on ../../../../../tf/modules/rbac/ec_kibana_data_views/main.tf line 1, in resource "elasticstack_kibana_data_view" "data_views":
│    1: resource "elasticstack_kibana_data_view" "data_views" {
│
│ The Terraform Provider unexpectedly returned no resource state after having no errors in the resource creation. This is always an issue in the Terraform Provider and should be reported to the provider developers.
│
│ The resource may have been successfully created, but Terraform is not tracking it. Applying the configuration again with no other action may result in duplicate resource errors. Import the resource if the resource was actually
│ created and Terraform should be tracking it.

**terraform plan**

  # module.rbac.module.kibana_data_views["prometheus"].elasticstack_kibana_data_view.data_views will be created
  + resource "elasticstack_kibana_data_view" "data_views" {
      + data_view = {
          + allow_no_index  = false
          + id              = (known after apply)
          + name            = "prom"
          + namespaces      = []
          + time_field_name = "@timestamp"
          + title           = "metrics-prometheus.remote_write*"
        }
      + id        = (known after apply)
      + override  = false
      + space_id  = "default"
    }

**terraform apply**

│ Error: Unable to create data view
│
│   with module.rbac.module.kibana_data_views["prometheus"].elasticstack_kibana_data_view.data_views,
│   on ../../../../../tf/modules/rbac/ec_kibana_data_views/main.tf line 1, in resource "elasticstack_kibana_data_view" "data_views":
│    1: resource "elasticstack_kibana_data_view" "data_views" {
│
│ Failed with: {"statusCode":400,"error":"Bad Request","message":"Duplicate data view: prom"}

Versions (please complete the following information):

tobio commented 3 weeks ago
│ Error: Unable to create data view
│
│   with module.rbac.module.kibana_data_views["prometheus"].elasticstack_kibana_data_view.data_views,
│   on ../../../../../tf/modules/rbac/ec_kibana_data_views/main.tf line 1, in resource "elasticstack_kibana_data_view" "data_views":
│    1: resource "elasticstack_kibana_data_view" "data_views" {
│
│ Failed with: {"statusCode":400,"error":"Bad Request","message":"Duplicate data view: prom"}

From the output it looks like the data view existed outside of Terraform before being applied. In that case you'll first want to import the data view into TF state and then start managing the existing data view. The TF docs have some more details about the import process.

x-dean commented 3 weeks ago

@tobio

That is the issue, the data view did not exist before, it is created during terraform apply and then it is not stored in state. On the next terraform apply it fails because terraform created it before and did not store in state.

Please read carefully before closing issues

Same bug: #620