Proper way to declare and pass variables from tfvars to root to module in TerraformOpenTofu without duplicating code - Stack Ove

What is the correct way to pass variables from the environment or .tfvars into the root module and on t

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
  • I think you shouldn't duplicate variables. In the main.tf you could just provide the value instead of creating another variable. – Marko E Commented Nov 21, 2024 at 15:27
  • I run often into this myself, ideally the validation should be everywhere the variable is used, but terraform makes hard especially when we decouple the modules and the main into multiple repos... my guess as the language matures they will introduce a concept similar to a class 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
  • I opened a new feature request: github/hashicorp/terraform/issues/36361 . . . feel free to add your thoughts there – Helder Sepulveda Commented Jan 17 at 17:10
Add a comment  | 

1 Answer 1

Reset to default 0

After 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742342478a4425904.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信