【问题标题】:Terraform 0.12 migration - Variables with default objectTerraform 0.12 迁移 - 具有默认对象的变量
【发布时间】:2021-02-02 09:22:14
【问题描述】:

我正在尝试将我的 TF 0.11 代码升级到 0.12,但遇到了一个变量问题。

在 TF 0.11 中,此阻塞在 variables.tf 文件中按预期工作

variable "postgres_dbs" {
  type = "map"

  default = {
    postgres1 = {
      name_postfix = "postgres",
      enable      = true,
      sku         = "MO_Gen5_16",
      capacity    = "16"
    }
    postgres2 = {
      name_postfix = "postgres-2",
      enable      = false,
      sku         = "MO_Gen5_16",
      capacity    = "16"
    }
    postgres3 = {
      name_postfix = "postgres-3",
      enable      = false,
      sku         = "MO_Gen5_16",
      capacity    = "16"
    }
    postgres4 = {
      name_postfix = "postgres-4",
      enable       = false,
      sku          = "MO_Gen5_16",
      capacity     = "16"
    }
    postgres5 = {
      name_postfix = "postgres-5",
      enable       = false,
      sku          = "MO_Gen5_16",
      capacity     = "16"
    }
    postgres6 = {
      name_postfix = "postgres-6",
      enable       = false,
      sku          = "MO_Gen5_16",
      capacity     = "16"
    }
    postgres7 = {
      name_postfix = "postgres-7",
      enable       = false,
      sku          = "MO_Gen5_16",
      capacity     = "16"
    }
    postgres8 = {
      name_postfix = "postgres-8",
      enable       = false,
      sku          = "MO_Gen5_16",
      capacity     = "16"
    }
  }
}

运行 terraform 012upgrade 命令后(它没有错误完成) TF改块使用

variable "postgres_dbs" {
  type = map(string)

但是运行 terraform validate 时的错误是:

This default value is not compatible with the variable's type constraint:
element "postgres6": string required.

有什么建议吗? 谢谢!

【问题讨论】:

  • 你能试试把map(string)改成map(map)吗?
  • 尝试并得到此错误:144:type = map(map) 地图类型构造函数需要一个参数来指定元素类型。
  • 看起来实际上是map(object)

标签: terraform


【解决方案1】:

您可以更新和微调您的类型规范,如下所示:

type = map(object({
  name_postfix = string
  enable       = bool
  sku          = string
  capacity     = string
}))

理想情况下,您可以将capacity 对象参数指定为number,但是您将输入到该参数的参数转换为string,因此除非您将输入指定为其自然@,否则将导致不兼容987654325@类型。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多