【问题标题】:terraform include parts of the definition of a resource block with a remote file templateterraform 包含具有远程文件模板的资源块定义的部分内容
【发布时间】:2022-01-07 17:00:50
【问题描述】:

是否可以包含要从远程文件注入的部分资源定义?例如,我通过使用页面 {} 块定义仪表板中包含的每个页面来使用 https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/one_dashboard 创建仪表板。

在下面的示例中,我在资源 {} 下有三个页面定义。是否可以替换我的每个或所有页面 {} 块定义以从远程文件获取内容?

预期定义

resource "newrelic_one_dashboard" "exampledash" {

  name = "New Relic Terraform Example with three pages"

  page {
      // reference to remote file with contents for page 1
  }

  page {
      // reference to remote file with contents for page 2
  }
  
  page {
      // reference to remote file with contents for page 3
  }  
}

OR

resource "newrelic_one_dashboard" "exampledash" {

  name = "New Relic Terraform Example with three pages"

   // reference to remote file with all page {} definitions

}

实际定义

resource "newrelic_one_dashboard" "exampledash" {

  name = "New Relic Terraform Example with three pages"

  page {
    name = "Page1"

    widget_bar {
      title = "Average transaction duration, by application"
      row = 1
      column = 1

      nrql_query {
        account_id = var.account_id
        query      = "FROM Transaction SELECT count(1) FACET appName"
      }
        filter_current_dashboard = true
    }

    widget_line {
        title = "response time view"
        row = 2
        column = 1
        width = 4
        height = 4
        # drilldown_dashboard_id = self.id
        nrql_query {
            account_id = var.account_id
            query      = "FROM Transaction SELECT average(duration) FACET appName"
        }            
    }    

  }

  page {
    name = "Page2"

    widget_bar {
      title = "Average transaction duration, by application"
      row = 1
      column = 1

      nrql_query {
        account_id = var.account_id
        query      = "FROM Transaction SELECT count(1) FACET appName"
      }
        filter_current_dashboard = true

    }

    widget_line {
        title = "response time view"
        row = 2
        column = 1
        width = 4
        height = 4
        # drilldown_dashboard_id = self.id
        nrql_query {
            account_id = var.account_id
            query      = "FROM Transaction SELECT average(duration) FACET appName"
        }            
    }    

  }

  page {
    name = "Page3"

    widget_bar {
      title = "Average transaction duration, by application"
      row = 1
      column = 1

      nrql_query {
        account_id = var.account_id
        query      = "FROM Transaction SELECT count(1) FACET appName"
      }
        filter_current_dashboard = true

    }

    widget_line {
        title = "response time view"
        row = 2
        column = 1
        width = 4
        height = 4
        # drilldown_dashboard_id = self.id
        nrql_query {
            account_id = var.account_id
            query      = "FROM Transaction SELECT average(duration) FACET appName"
        }            
    }    

  }    
}

期待

【问题讨论】:

  • 不在 TF 中。也许使用 Terragrunt 你可以做到这一点。

标签: terraform newrelic terraform-template-file


【解决方案1】:

我不熟悉您正在使用的具体资源,但我会从 file 函数开始。

我过去是这样使用它的:

data "template_file" "policy" {
  count    = "${var.enabled == "true" ? 1 : 0}"
  template = "${file(./filename.json)}"

  vars = "${var.iam-policy-file-vars}"
}

【讨论】:

  • 我确实看过 File 和 templatefile 函数。我的要求是将整个资源块保留在代码中,但仅包含来自远程文件的 page {} 块。从文档中,文件引用必须是本地的,并且必须位于键值对的右侧。但是如果我理解正确就不能直接嵌入
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 2022-12-23
  • 2020-08-22
  • 2014-11-13
  • 2014-04-08
  • 1970-01-01
相关资源
最近更新 更多