user389955
user389955

Reputation: 10487

Cannot run Terraform google_monitoring_dashboard which has Template file with GCP monitoring Dashboard Variables

I created a GCP monitoring dashboard manually in GCP console, and then I copy the json file of the manually created dashboard.

Then I run a terraform code to create gcp monitoring dashboard using template of tpl, which is the above json file.

Here is the terraform code:

locals {
  dashboard_template = "${path.module}/dashboard_definition.json.tpl"
}
resource "google_monitoring_dashboard" "test" {
  project = var.project
  dashboard_json = templatefile(local.dashboard_template, {
    title                            = var.dashboard_title
  })
}

the json file dashboard_definition.json.tpl is as below. it is generated from GCP console's monitoring dashboard.

  "displayName": "${title}",
  "dashboardFilters": [],
  "mosaicLayout": {
    "columns": 48,
    "tiles": [
      {
        "widget": {
          "title": "XXX",
            "dataSets": [
              {
                "legendTemplate": "${metric.labels.role} (average)(${resource.labels.cluster_id})",
                "minAlignmentPeriod": "300s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "300s",
                      "perSeriesAligner": "ALIGN_MEAN"
                    },
                    "filter": ...
                  }
                }
              },

However, when planing terraform, I get error, i.e. terraform does not know what is ${metric.labels.role}. In fact, in GCP dashboard console, ${metric.labels.role} is variable for primary or replica, in fact terraform does not understand ${resource.labels.cluster_id} either, although the variables are recognized in GCP monitoring dashboard, and the template file is generated from GCP monitoring dashboard.

How to make terraform understand the variable used in GCP dashboard I created manually?

Error: Invalid function argument

in resource "google_monitoring_dashboard" "test":
   6:   dashboard_json = templatefile(local.dashboard_template, {
   7: 
   8: 

Invalid value for "vars" parameter: vars map does not contain key "metric",
referenced at .terraform/modules/dashboard_definition.json.tpl:19,38-44.

Upvotes: 0

Views: 160

Answers (0)

Related Questions