admin管理员组文章数量:1332689
What is the correct way to pass variables from the environment or .tfvars
into the root module and on to sub-modules? I know this question is borderline opinion, but I feel there has to be a clear, best-practice on this.
Say, I have the following "complex" object defined in .tfvars
:
example = [
{
name = "example1"
size = {
height = 10
length = 20
}
},
{
name = "example2"
size = {
height = 30
length = 40
}
}
]
Now, should I declare this complex data type, validations, and defaults in the main.tf
? The module.tf
? Or both?
For example, this could be the variable declaration:
variable "example" {
type = list(object({
name = string
size = {
height = number
length = number
}
}))
nullable = false
description = "Example complex object"
validation {
condition = example.size.height > 5
error_message = "Your height is expected to be greater than 5."
}
}
Now, it doesn't make sense to me that I would declare this level of complexity and checks in the main.tf
AND in the module.tf
and duplicate code. Especially considering the module.tf
may be re-used in other projects.
Instead, I could declare something like this in the main.tf
:
variable "example" {
type = list(object())
nullable = false
description = "Example complex object."
}
and let the module.tf
actually validate the variable.
What is the correct way to do this?
What is the correct way to pass variables from the environment or .tfvars
into the root module and on to sub-modules? I know this question is borderline opinion, but I feel there has to be a clear, best-practice on this.
Say, I have the following "complex" object defined in .tfvars
:
example = [
{
name = "example1"
size = {
height = 10
length = 20
}
},
{
name = "example2"
size = {
height = 30
length = 40
}
}
]
Now, should I declare this complex data type, validations, and defaults in the main.tf
? The module.tf
? Or both?
For example, this could be the variable declaration:
variable "example" {
type = list(object({
name = string
size = {
height = number
length = number
}
}))
nullable = false
description = "Example complex object"
validation {
condition = example.size.height > 5
error_message = "Your height is expected to be greater than 5."
}
}
Now, it doesn't make sense to me that I would declare this level of complexity and checks in the main.tf
AND in the module.tf
and duplicate code. Especially considering the module.tf
may be re-used in other projects.
Instead, I could declare something like this in the main.tf
:
variable "example" {
type = list(object())
nullable = false
description = "Example complex object."
}
and let the module.tf
actually validate the variable.
What is the correct way to do this?
Share Improve this question asked Nov 20, 2024 at 17:01 AppleoddityAppleoddity 1,1811 gold badge13 silver badges41 bronze badges 3 |1 Answer
Reset to default 0After replying in the comments I noticed that your example has a few issues:
- The
size = {
is not valid syntax - The validation for a list will need a loop
working code:
variable "example" {
description = "Example complex object"
type = list(object({
name = string
size = object({
height = number
length = number
})
}))
validation {
condition = alltrue([
for v in var.example : v.size.height > 5
])
error_message = "Your height is expected to be greater than 5."
}
}
My answer does not address your main concern of reusability, that is a feature that hopefully will be added to terraform in the near future
本文标签:
版权声明:本文标题:Proper way to declare and pass variables from tfvars to root to module in TerraformOpenTofu without duplicating code - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742342478a2456880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
main.tf
you could just provide the value instead of creating another variable. – Marko E Commented Nov 21, 2024 at 15:27class
and all we will need to do is set the type to that class and the same structure and validation applies – Helder Sepulveda Commented Jan 17 at 16:53