简体   繁体   中英

Difference between *.tfvars file and passing values in main.tf in terraform

why some values are passed in main.tf file instead of *.tfvars files while calling a module. I understand variables.tf is to define variables along with any default values. *.tfvar file is to pass values for variables. but they also pass some value for variables in main.tf of where we call that module. why is it so? any help, please.

googled did not get a satisfying answer.

main.tf

   module =vpc
   source = ./path/vpc
    cidr_block = 11.0.0.0/16

but they also have *.tfvars file. Instead of passing the value there why is it necessary to give here

A reasonable analogy for Terraform Input Variables is to function parameters in a general-purpose programming language. Just as with function parameters, Terraform Input Variables have two parts: the declaration and the definition.

The declaration says that the variable exists, sets a type for it, and sets optionally sets default value for it (which, if set, makes the variable optional):

variable "cidr_block" {
  type    = string
  default = "10.0.0.0/16"
}

The definition assigns it a specific value. In Terraform's case, for the root module it is -var command line arguments and .tfvars files that define the variables. For a child module it's the key/value pairs inside the module block that called the module.


An example of a general-purpose programming language that has a concept of default values for parameters is Python, so a Python example might be a helpful analogy for those who are familiar with it:

def example(cidr_block="10.0.0.0/16"):
    pass

Similarly to Terraform, we can choose to call this function as example() and let the default value apply. We could also call example("11.0.0.0/16") to override that default. The third possibility is that we call example("10.0.0.0/16") and override the default to the same value .

From Python's perspective, there is no functional difference between calling example() and calling example("10.0.0.0/16") here, but as authors of the caller we can use this to express subtle intent: if we leave it unset then we're saying "I'm happy with whatever the function chooses; the exact value is irrelevant", while if we set the argument explicitly to the same value as the default we're saying "It's important for this caller that we always select "10.0.0.0/16" here, regardless of what the default might be".

The decision-making process for whether to explicitly define a Terraform Input Variable is similar: if the specific chosen value is not crucial for the calling module then we might as well just let the default be selected. But if having the explicit definition will help a future reader understand the intent better, we might choose to explicitly define it.

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