Terraform 0.12迁移:默认对象变量类型不兼容问题求助
Terraform 0.11 → 0.12 升级:postgres_dbs 变量类型不兼容问题解决
这个问题的核心是Terraform 0.12对类型系统做了严格强化,而012upgrade工具没能准确识别你原来嵌套对象结构的map类型,自动生成了错误的map(string)约束,和实际默认值的类型不匹配。
解决步骤:
- 修正变量类型约束:你的
postgres_dbs是一个值为嵌套对象的map,需要把类型定义为map(object({...})),明确每个元素的结构和属性类型。 - 恢复默认值:
012upgrade工具可能移除了你的默认值配置,需要把原来的default块加回去,确保和新的类型约束匹配。
修正后的完整变量块如下:
variable "postgres_dbs" { type = map(object({ name_postfix = string enable = bool sku = string capacity = string })) 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 0.11中,变量类型是松散的,type = "map"仅表示这是一个map结构,不限制值的类型。而0.12引入了强类型系统,012upgrade工具处理嵌套结构时,默认推断成了map(string)(值为字符串的map),但你实际的默认值里每个元素都是包含多个属性的对象,因此出现了类型不兼容的报错。
完成上述修改后,再运行terraform validate就能顺利通过了。
内容的提问来源于stack exchange,提问作者Amit Daniel
相关产品推荐
相关产品推荐

