【问题标题】:terraform count index and lengthterraform 计数索引和长度
【发布时间】:2021-08-10 20:03:22
【问题描述】:

尝试创建多个私有 DNS 区域

  1. 非产品 --> Dev、QA、UAT。
  2. prod --> PRd,DR

仅当 is_nonprod 设置为 1 时才应创建资源。强调文本(布尔值)。 想法是在资源块中使用两次计数:一次用于布尔值,一次用于长度函数。

resource "azurerm_private_dns_zone" "example" {
      
      count                  = var.is_nonprod ? 1 : 0 && length(var.env)
      name                   = var.env[count.index].npr
      resource_group_name    = "examplerg"
    }

变量文件:

variable "env" {
  description = "List of routes to be added to the route table"
  default     = []
  type        = list(map(any))
}

variable "is_nonprod " {
    default = true
}

tfvars

env = [
  { npr = "qa" },
  { npr = "uat" },
  { npr = "dev" }
]

错误: 真假结果表达式的类型必须一致。给定的 表达式分别是数字和布尔值。

解决方法:

resource "azurerm_private_dns_zone" "example" {
      
      count                  = var.is_nonprod ? 1 : 0 
      count                  = length(var.env)
      name                   = var.env[count.index].npr
      resource_group_name    = "examplerg"
    }

错误: 参数“count”已经设置在 main.tf:96,3-8。每个参数可能是 只设置一次。

【问题讨论】:

    标签: azure terraform


    【解决方案1】:

    var.is_nonprod ? 1 : 0 && length(var.env) 根据您在问题中描述的内容,这里看起来不像您想要的逻辑。看起来你真的想要var.is_nonprod ? length(var.env) : 0,这在语法上也是有效的。 && 运算符输入并返回布尔值,这些布尔值作为count 元参数的输入类型无效。 count 将数字作为输入(通常是您要管理的资源数量),而不是 truefalse

    【讨论】:

      【解决方案2】:

      看起来像数据类型冲突。您可以直接使用count = var.is_nonprod ? length(var.env) : 0 ,它应该会给您预期的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 2017-10-13
        • 2018-02-22
        • 2017-06-15
        • 2016-08-31
        • 1970-01-01
        相关资源
        最近更新 更多