# Creating opensearch dashboard visualisations/dashboards via terraform

**URL:** <https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289>\
**Category:** OpenSearch Dashboards\
**Created:** [November 4, 2024, 1:00pm UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289 "2024-11-04T13:00:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will29](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@Will29](https://forum.opensearch.org/u/Will29)\
**Post date:** [November 4, 2024, 1:00pm UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289/1 "2024-11-04T13:00:01Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
OpenSearch 2.15

**Describe the issue** :  
I have set up an opensearch domain/pipeline/dashboard in terraform and then also created an index pattern in terraform that i can set up queries/visualisations against. When i create visualisations against this manually in the opensearch dashboards console this works, however i have been trying to replicate this metric creation in terraform as i need to implement this across multiple accounts. here is the resource block to do this:

```auto
resource “opensearch_dashboard_object” “visualization” {
tenant_name = “tenant”
body = <<EOF
[
{
“_id”: “visualization:${var.account_alias}-daily-device-numbers”,
“_type”: “doc”,
“_source”: {
“type”: “visualization”,
“visualization”: {
“title”: “Daily device numbers”,
“visState”: “{"title":"daily device numbers","type":"metric","params":{"addTooltip":true},"aggs":[{"id":"1","enabled":true,"type":"count","schema":"metric"}]}”,
“uiStateJSON”: “{}”,
“description”: “count the number of devices in certain categories”,
“version”: 1
}
}
}
]
EOF
}

```

**Relevant Logs or Screenshots** :  
However when i try to view this in the visualization in the opensearch console i get the following error (the index pattern i created is the default index pattern so it should target this) - Failed to load the visualization, Trying to initialize aggs without index pattern

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/e/e888c6d4d13601b4a9daee58535bd6a6b17100b9.png)

---

<div class="post-metadata">

**Author:** ![pablo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/pablo/32/10363_2.png) [@pablo](https://forum.opensearch.org/u/pablo)\
**Post date:** [December 13, 2024, 2:06pm UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289/2 "2024-12-13T14:06:17Z")

</div>

@Will29 You need to assign index pattern to your visualization.  
Take a look at my Terraform example.

```auto
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
}

```

You can find index pattern ID in its URL.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/4/4dee2a5b780a97348863dd78d6ee7e8fb015cc57.png)

---

<div class="post-metadata">

**Author:** ![Will29](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@Will29](https://forum.opensearch.org/u/Will29)\
**Post date:** [December 16, 2024, 4:59pm UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289/3 "2024-12-16T16:59:47Z")

</div>

Thanks @pablo but I don’t think that will change anything, if a index pattern isn’t assigned it will use the default index pattern, (and in my situation I had only created one index pattern which was what I wanted the visualizations to point to)

---

<div class="post-metadata">

**Author:** ![pablo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/pablo/32/10363_2.png) [@pablo](https://forum.opensearch.org/u/pablo)\
**Post date:** [December 17, 2024, 9:49am UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289/4 "2024-12-17T09:49:30Z")

</div>

@Will29 According to my tests, the default index pattern is not picked when I create visualization without specifying an index pattern.

```auto
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
      }
    }
  }
]
EOF
}

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/f/fd31f13af0c48939fe64f2a7645624515b62d920.png)

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/f/f2eb08bf65f693ea6e7c957828d86aa9361fff2a.png)

---

<div class="post-metadata">

**Author:** ![lucyy300](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@lucyy300](https://forum.opensearch.org/u/lucyy300)\
**Post date:** [December 18, 2024, 12:50pm UTC](https://forum.opensearch.org/t/creating-opensearch-dashboard-visualisations-dashboards-via-terraform/22289/5 "2024-12-18T12:50:38Z")

</div>

> [@Will29](#):
>
> I have set up an opensearch domain/pipeline/dashboard in terraform and then also created an index pattern in terraform that i can set up queries/visualisations against. When i create visualisations against this manually in the opensearch dashboards console this works, however i have been trying to replicate this metric creation in terraform as i need to implement this across multiple accounts. [https://atelierrebul.hr/](https://atelierrebul.hr/) here is the resource block to do this:

The `indexPattern` you created will be necessary for associating with the visualizations.
