简体   繁体   中英

How to reuse variable *definitions* in Terraform?

If I create a varaible definition like this:

variable "aws_ecs_config" {
  type = object({
    cpu               = number
    memory            = number
    ecs_image_address = string
  })
  logs = {
    type = object({
      group         = string
      region        = string
      stream_prefix = string
    })
  }
}

How can I reuse that definition in multiple places without copy-pasting?

It is not possible to re-use variable declarations in Terraform. If variables in different modules will have the same type, that type must be repeated in each module.

Terraform has a structural type system rather than a nominal type system , so types themselves are not named and instead are matched/constrained by their form. When passing values between modules, we can use type constraints to create conventions between a family of related modules, but there is no mechanism to define a type (or declare a variable) in one place and reuse it in other places.


Terraform's type constraint mechanism considers any object with at least the attributes in the type constraint to be valid, so it's not necessary to define an exhaustive object type every time.

For example, if you define a variable with the following type:

object({
  name = string
})

The following object value is acceptable for that type constraint, because it has a name attribute of the correct type regardless of any other attributes it defines:

{
  name  = "foo"
  other = "bar"
}

For that reason, it can be better to limit the variable declaration in each module to only the subset of attributes that particular module actually requires, which reduces the coupling between the modules: they only need to be compatible to the extent that their attribute names overlap, and don't need to directly bind to one another.

Workaround

This is possible without explicit Terraform support. (There are some discussions about types. Weigh in if you think it would be handy)

Terraform allows variables to be declared over multiple files, and if the modules live on a Linux filesystem, reuse is vastly simplified. (Windows folks... well, it is possible , just not pretty and may not play well with version control.)

How?

  • Move the variable definition to a file named variable.aws_ecs_config.tf
  • Place it in a well known place. Say, a directory called variables that lives next to modules .
  • Whenever the variable is needed in a module, create a symlink to the variable file: ln -s <path/to/variables>/variable.aws_ecs_config.tf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM