Search code examples
terraformterraform-provider-azureterraform-template-file

terraform templatefile Function Invalid template interpolation value(AKS Cluster Name)


I try to provision an azure dashboard using terraform module and template file but I get an error message:

Call to function "templatefile" failed:
src/main/terraform/environment/modules/someservice/resources/dashboards/infrastructure-dashboard.tpl:41,39-55:
Invalid template interpolation value; Cannot include the given value in a
string template: string required., and 25 other diagnostic(s).

The error is being caused by the code in the template. It does not accept the variable declared in the dashboard properties. The code in the template causing the error:

"clusterName": "${k8s_cluster_name}",


Terraform code:
resource "azurerm_dashboard" "infra-dashboard" {
  name                = "${upper(terraform.workspace)}-Infrastructure"
  resource_group_name = azurerm_resource_group.rgp-dashboards.name
  location            = azurerm_resource_group.rgp-dashboards.location
  tags = {
    source = "terraform"
  }
  dashboard_properties = templatefile("${path.module}/resources/dashboards/infrastructure-dashboard.tpl",
    {
      md_content                  = var.infra_dashboard_md_content,
      dashboard_title             = var.infra_dashboard_title,
      dashboard_subtitle          = var.infra_dashboard_subtitle,
      sub_id                      = data.azurerm_subscription.current.subscription_id,
      rgp_k8s                     = azurerm_resource_group.seervice-k8s.name,
      k8s_cluster_name            = azurerm_kubernetes_cluster.service-k8s.*.name,
      rgp_service_env_global_name   = azurerm_resource_group.service-env-global.name,
      log_analyt_wrkspc              = local.log_analyt_wrkspc
  })
}

Snippet from the tmeplate where the error occurs:
"1": {
            "position": {
              "x": 0,
              "y": 4,
              "colSpan": 9,
              "rowSpan": 4
            },
            "metadata": {
              "inputs": [
                {
                  "name": "queryParams",
                  "value": {
                    "metricQueryId": "node-count",
                    "clusterName": "${k8s_cluster_name}",
                    "clusterResourceId": "/subscriptions/${sub_id}/resourceGroups/${rgp_k8s}/providers/Microsoft.ContainerService/managedClusters/${k8s_cluster_name}",
                    "workspaceResourceId": "/subscriptions/${sub_id}/resourcegroups/${rgp_service_env_global_name}/providers/microsoft.operationalinsights/workspaces/${log_analyt_wrkspc}",
                    "timeRange": {
                      "options": {},
                      "relative": {
                        "duration": 21600000
                      }
                    },
                    "cpuFilterSelection": "total",
                    "memoryFilterSelection": "total_memoryrss"
                  }
                }

Solution

  • As luk2302 said azurerm_kubernetes_cluster.service-k8s.*.name it is not a string. In fact it is a tuple