Terraform/Terragrunt 如果我不添加默认变量会出错
Terraform/Terragrunt error if I don't add a default variable
我是 terraform 新手。我为云端编写了一个模块。我想将我的原始配置作为对象传递。当我 运行 terrag运行t 时,我得到这个错误:
Error: Invalid default value for variable
on variables.tf line 9, in variable "origin_config":
9: default = {
10: protocol_policy = "https-only"
11: ssl_protocol = ["TLSv1.2"]
12: http_port = 80
13: https_port = 443
14: }
This default value is not compatible with the variable's type constraint:
attribute "domain_name" is required.
这是我的模块代码:
resource "aws_cloudfront_distribution" "this" {
# origin config
origin {
domain_name = var.origin_config.domain_name
origin_id = local.origin_id
custom_origin_config {
origin_protocol_policy = var.origin_config.protocol_policy
origin_ssl_protocols = var.origin_config.ssl_protocol
http_port = var.origin_config.http_port
https_port = var.origin_config.https_port
}
}
这是我的 variables.tf
variable "origin_config" {
type = object({
domain_name = string
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = 80
https_port = 443
}
}
这是我的 terraform.tfvars:
origin_config = {
domain_name = "test.example.com"
}
如果我将变量 domain_name
添加为默认值,那么它就可以工作。似乎不知何故没有从 terraform.tvars?
读取我的输入
您的 type
包括 domain_name = string
,因此 必须 。因此,如果您不想设置固定值,则必须设置一个空字符串,例如:
variable "origin_config" {
type = object({
domain_name = string
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
domain_name = ""
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = "80"
https_port = "443"
}
}
否则,从您的 type
中删除 domain_name
。还有experimentaloptional关键词:
terraform {
experiments = [module_variable_optional_attrs]
}
variable "origin_config" {
type = object({
domain_name = optional(string)
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = "80"
https_port = "443"
}
}
我的问题原来是,通过尝试在我的 tfvars 中指定单个值,我覆盖了包含我的默认值的对象。
解决方案是合并这两个对象,这样我的单个 key:value 输入将与默认对象合并为一个新对象。
这里有一个很好的解释:https://binx.io/blog/2020/01/02/module-parameter-defaults-with-the-terraform-object-type/
我是 terraform 新手。我为云端编写了一个模块。我想将我的原始配置作为对象传递。当我 运行 terrag运行t 时,我得到这个错误:
Error: Invalid default value for variable
on variables.tf line 9, in variable "origin_config":
9: default = {
10: protocol_policy = "https-only"
11: ssl_protocol = ["TLSv1.2"]
12: http_port = 80
13: https_port = 443
14: }
This default value is not compatible with the variable's type constraint:
attribute "domain_name" is required.
这是我的模块代码:
resource "aws_cloudfront_distribution" "this" {
# origin config
origin {
domain_name = var.origin_config.domain_name
origin_id = local.origin_id
custom_origin_config {
origin_protocol_policy = var.origin_config.protocol_policy
origin_ssl_protocols = var.origin_config.ssl_protocol
http_port = var.origin_config.http_port
https_port = var.origin_config.https_port
}
}
这是我的 variables.tf
variable "origin_config" {
type = object({
domain_name = string
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = 80
https_port = 443
}
}
这是我的 terraform.tfvars:
origin_config = {
domain_name = "test.example.com"
}
如果我将变量 domain_name
添加为默认值,那么它就可以工作。似乎不知何故没有从 terraform.tvars?
您的 type
包括 domain_name = string
,因此 必须 。因此,如果您不想设置固定值,则必须设置一个空字符串,例如:
variable "origin_config" {
type = object({
domain_name = string
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
domain_name = ""
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = "80"
https_port = "443"
}
}
否则,从您的 type
中删除 domain_name
。还有experimentaloptional关键词:
terraform {
experiments = [module_variable_optional_attrs]
}
variable "origin_config" {
type = object({
domain_name = optional(string)
protocol_policy = string
ssl_protocol = list(string)
http_port = number
https_port = number
})
default = {
protocol_policy = "https-only"
ssl_protocol = ["TLSv1.2"]
http_port = "80"
https_port = "443"
}
}
我的问题原来是,通过尝试在我的 tfvars 中指定单个值,我覆盖了包含我的默认值的对象。
解决方案是合并这两个对象,这样我的单个 key:value 输入将与默认对象合并为一个新对象。
这里有一个很好的解释:https://binx.io/blog/2020/01/02/module-parameter-defaults-with-the-terraform-object-type/