Terraform Dashboard objects

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
OpenSearch 2.13 (latest)

Describe the issue:
Context: So I am exporting cloudtrail logs into the opensearch cluster. And I have a difficult time understanding the correct terraform configuration for the JSON

https://registry.terraform.io/providers/opensearch-project/opensearch/latest/docs/resources/dashboard_object

I created some object with the UI and now im trying to accomplish this with terraform. In opensearch you can see your JSON configuration in Dashboard management > Saved objects > Edit visualization but Im very unsure how to configure that into the Terraform JSON cause to me they dont seems the same maybe im missing some documentation?

Configuration:
If we take and UI Created dashboad object as an example we get these 3 json objects:

visState

{
  "title": "Source Ip Addresses",
  "type": "table",
  "aggs": [
    {
      "id": "1",
      "enabled": true,
      "type": "count",
      "params": {},
      "schema": "metric"
    },
    {
      "id": "2",
      "enabled": true,
      "type": "significant_terms",
      "params": {
        "field": "sourceIPAddress.keyword",
        "size": 500,
        "customLabel": "Source Ip addresses"
      },
      "schema": "bucket"
    }
  ],
  "params": {
    "perPage": 10,
    "showPartialRows": false,
    "showMetricsAtAllLevels": false,
    "showTotal": false,
    "totalFunc": "sum",
    "percentageCol": ""
  }
}

kibanaSavedObjectMeta.searchSourceJSON


{
  "query": {
    "query": "",
    "language": "kuery"
  },
  "filter": [
    {
      "$state": {
        "store": "appState"
      },
      "exists": {
        "field": "sourceIPAddress"
      },
      "meta": {
        "alias": null,
        "disabled": false,
        "key": "sourceIPAddress",
        "negate": false,
        "type": "exists",
        "value": "exists",
        "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index"
      }
    }
  ],
  "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.index"
}

references

[
  {
    "name": "kibanaSavedObjectMeta.searchSourceJSON.index",
    "type": "index-pattern",
    "id": "035e2760-24d9-11ef-962f-672ae61b9dd9"
  },
  {
    "name": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index",
    "type": "index-pattern",
    "id": "035e2760-24d9-11ef-962f-672ae61b9dd9"
  }
]

But I dont understand how that translate to this example (from terraform) cause the visState is defined but where should the kibanaSavedObjectMeta.searchSourceJSON and references be?:

resource "opensearch_dashboard_object" "test_visualization_v6" {
  body = <<EOF
[
  {
    "_id": "visualization:response-time-percentile",
    "_type": "doc",
    "_source": {
      "type": "visualization",
      "visualization": {
        "title": "Total response time percentiles",
        "visState": {<Here comes the visState>},
        "uiStateJSON": "{}",
        "description": "",
        "version": 1
      }
    }
  }
]
EOF
}

Im sorry if im missing something but all help would be appreciated

@ActivelyHappy Have you tried adding kibanaSavedObjectMeta.searchSourceJSON as per ES Terraform example?

https://registry.terraform.io/providers/0xDEADEAD/elasticsearch/latest/docs/resources/kibana_object

i.e.

resource "opensearch_dashboard_object" "source_ip_addresses" {
  tenant_name = "tenant"
  body        = <<EOF
[
  {
    "_id": "source-ip-addresses",
    "_source": {
      "type": "visualization",
      "visualization": {
        "title": "Source Ip Addresses",
        "visState": "{\"title\":\"Source Ip Addresses\",\"type\":\"table\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"params\":{},\"schema\":\"metric\"},{\"id\":\"2\",\"enabled\":true,\"type\":\"significant_terms\",\"params\":{\"field\":\"sourceIPAddress.keyword\",\"size\":500,\"customLabel\":\"Source Ip addresses\"},\"schema\":\"bucket\"}],\"params\":{\"perPage\":10,\"showPartialRows\":false,\"showMetricsAtAllLevels\":false,\"showTotal\":false,\"totalFunc\":\"sum\",\"percentageCol\":\"\"}}",
        "uiStateJSON": "{}",
        "description": "",
        "version": 1,
        "kibanaSavedObjectMeta": {
          "searchSourceJSON": "{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[{\"$state\":{\"store\":\"appState\"},\"exists\":{\"field\":\"sourceIPAddress\"},\"meta\":{\"alias\":null,\"disabled\":false,\"key\":\"sourceIPAddress\",\"negate\":false,\"type\":\"exists\",\"value\":\"exists\",\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index\"}}],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"
        }
      },
      "references": [
        {
          "name": "kibanaSavedObjectMeta.searchSourceJSON.index",
          "type": "index-pattern",
          "id": "035e2760-24d9-11ef-962f-672ae61b9dd9"
        },
        {
          "name": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index",
          "type": "index-pattern",
          "id": "035e2760-24d9-11ef-962f-672ae61b9dd9"
        }
      ]
    }
  }
]
EOF
}

I have tried to add references to the dashboard object, but it never gets created by the terraform provider. Which makes the visualization useless.

Here is the resource code:

resource "opensearch_dashboard_object" "my_test_visualization" {
  body = <<EOF
[
  {
    "_id": "visualization:my-test-visualization",
    "_type": "doc",
    "_source": {
      "type": "visualization",
      "visualization": {
        "title": "My Test Visualization",
        "visState": "{\"title\":\"My Test Visualization\",\"type\":\"gauge\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"params\":{},\"schema\":\"metric\"}],\"params\":{\"type\":\"gauge\",\"addTooltip\":true,\"addLegend\":true,\"isDisplayWarning\":false,\"gauge\":{\"alignment\":\"automatic\",\"extendRange\":true,\"percentageMode\":false,\"gaugeType\":\"Arc\",\"gaugeStyle\":\"Full\",\"backStyle\":\"Full\",\"orientation\":\"vertical\",\"colorSchema\":\"Green to Red\",\"gaugeColorMode\":\"Labels\",\"colorsRange\":[{\"from\":0,\"to\":50},{\"from\":50,\"to\":75},{\"from\":75,\"to\":100}],\"invertColors\":false,\"labels\":{\"show\":false,\"color\":\"black\"},\"scale\":{\"show\":true,\"labels\":false,\"color\":\"rgba(105,112,125,0.2)\"},\"type\":\"meter\",\"style\":{\"bgWidth\":0.9,\"width\":0.9,\"mask\":false,\"bgMask\":false,\"maskBars\":50,\"bgFill\":\"rgba(105,112,125,0.2)\",\"bgColor\":true,\"subText\":\"\",\"fontSize\":60}}}}",
        "uiStateJSON": "{}",
        "description": "",
        "version": 1
      }
    },
    "references": [
        {
            "id": "7cf59080-f5c3-11ee-8f15-93fad418634e",
            "name": "kibanaSavedObjectMeta.searchSourceJSON.index",
            "type": "index-pattern"
        },
        {
            "id": "7cf59080-f5c3-11ee-8f15-93fad418634e",
            "name": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index",
            "type": "index-pattern"
        }
    ]
  }
]
EOF
}

Any help on this?

I have been facing the same issue any resolution on this ??
After creation of visualizations without references and kibanaSavedObjectMeta.searchSourceJSON the visualtions doesn’t load

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

@vcaetano @psouvik69 I’m not sure if this is still an issue for you but I’ve got it working.
@vcaetano If you take a look at my example, it has kibanaSavedObjectMeta.searchSourceJSON with the kibanaSavedObjectMeta.searchSourceJSON.index in the value (end of the line).
This connects the reference.name and the index template itself. Your code example is missing that part.

Take a look at my working example.

terraform {
  required_providers {
    opensearch = {
      source  = "opensearch-project/opensearch"
      version = "~> 2.0"
    }
  }
}
provider "opensearch" {
  # Adjust with your OpenSearch cluster's endpoint and credentials
  url      = var.opensearch_url
  username = var.opensearch_username
  password = var.opensearch_password
  insecure = true
}

resource "opensearch_dashboard_object" "test_visualization_v7" {
  body        = <<EOF
[
  {
    "_id": "visualization:response-time-percentile_v1",
    "_type": "doc",
    "_source": {
      "type": "visualization",
      "visualization": {
        "title": "Total response time percentiles2",
        "visState": "{\"title\":\"vis1\",\"type\":\"table\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"params\":{},\"schema\":\"metric\"}],\"params\":{\"perPage\":10,\"showPartialRows\":false,\"showMetricsAtAllLevels\":false,\"showTotal\":false,\"totalFunc\":\"sum\",\"percentageCol\":\"\"}}",

        "uiStateJSON": "{}",
        "description": "",
        "version": 1,
        "kibanaSavedObjectMeta": {
          "searchSourceJSON": "{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"
        }

      },
      "references": [
        {
          "name": "kibanaSavedObjectMeta.searchSourceJSON.index",
          "type": "index-pattern",
          "id": "37c65470-b933-11ef-ad85-817902cf53ce"
        }
      ]
    }
  }
]
EOF
}