Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass list variable to JSON template in terraform

I am trying to create a cloudwatch dashboard in terraform that would be configurable via variables. I realize that dashboard body is a JSON string, so basic interpolation (like "region" property below) works. But with something like "metrics" I need to pass a list - and terraform throws "invalid interpolation" error. Is there a way to have configurable template where I can pass complex variables?

resource "aws_cloudwatch_dashboard" "dashboard" {
  dashboard_name = "dashboard"

  dashboard_body = <<EOF
  {
    "widgets": [
      {
        "type": "metric",
        "width": 12,
        "properties": {
          "metrics": ${local.database_metrics},
          "region": "${var.aws_region}"
        }
      }
    ]
  }
  EOF
}
like image 946
Yuriy Galanter Avatar asked Sep 03 '25 04:09

Yuriy Galanter


1 Answers

Terraform has a jsonencode function.

like image 159
Mark B Avatar answered Sep 04 '25 20:09

Mark B