Azure CDN Terraform变量:如何定义含list(string)与string的混合类型?
问题
在为Azure CDN配置Terraform交付规则时,delivery_rules变量中的request_scheme_condition包含两个参数:match_value(字符串列表类型list(string))和operator(字符串类型string)。当前将request_scheme_condition的类型定义为map(list(string)),导致默认值与类型约束不兼容,触发报错。
错误代码:
variable "delivery_rules" { default = { name = "test" order = 1 request_scheme_condition = { match_value = ["HTTP"] operator = "Equal" } } type = object( { name = string order = number request_scheme_condition = map(list(string)) } ) }
报错信息:
│ Error: Invalid default value for variable │ │ on variables.tf line 92, in variable "delivery_rules": │ 92: default = { │ 93: name = "test" │ 94: order = 1 │ 95: request_scheme_condition = { │ 96: match_value = "HTTP" │ 97: │ 98: operator = "Equal" │ 99: } │ 100: } │ │ This default value is not compatible with the variable's type constraint: attribute "request_scheme_condition": element "
需解决:如何在单个变量块中正确定义这种混合类型的变量?
解决方案
问题核心是request_scheme_condition的类型定义错误:map(list(string))要求所有属性值都是字符串列表,但operator是单个字符串,不符合约束。正确做法是为request_scheme_condition定义嵌套对象类型,明确指定每个属性的类型:
variable "delivery_rules" { default = { name = "test" order = 1 request_scheme_condition = { match_value = ["HTTP"] operator = "Equal" } } type = object({ name = string order = number request_scheme_condition = object({ match_value = list(string) operator = string }) }) }
说明
- 将
request_scheme_condition的类型从map(list(string))改为嵌套object({...}),在内部分别定义match_value为list(string)、operator为string,完全匹配默认值的结构和类型。 - 此定义让Terraform能准确识别每个属性的类型,彻底解决类型不兼容的报错。
内容的提问来源于stack exchange,提问作者hari989
相关产品推荐
相关产品推荐

