Terraform 0.12 migration - Variables with default object

Viewed 1819

I'm trying to upgrade my TF 0.11 code to 0.12 and i stuck with a variable issue.

In TF 0.11 this blocked worked as expected in the variables.tf file

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"
    }
  }
}

After running the terraform 012upgrade command ( It finished with no errors ) TF changed the block to use

variable "postgres_dbs" {
  type = map(string)

But the error when running terraform validate is :

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

Any advice ? Thanks!

1 Answers

You can update and fine-tune your type specification like the following:

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

Ideally you could specify the capacity object argument as a number instead, but you are casting your inputs to that argument as a string, so that would cause an incompatibility unless you specified the input as its natural number type.

Related