Terraform noob here. Quick question. We have a project where we have variables defined in variables.tf(magma/variables.tf at master · magma/magma · GitHub) and I’m trying to override the required variables by using tfvars file.
root@ac19040aa1c7:~/tf# terraform plan -target=module.orc8r -var-file="infra.tfvars.json"
Warning: Value for undeclared variable
The root module does not declare a variable named "orc8r_domain_name" but a
value was found in file "infra.tfvars.json". To use this value, add a
"variable" block to the configuration.
Using a variables file to set an undeclared variable is deprecated and will
become an error in a future release. If you wish to provide certain "global"
settings to all configurations in your organization, use TF_VAR_...
environment variables to set these instead.
....
Error: Missing required argument
on main.tf line 19, in module "orc8r-app":
19: module "orc8r-app" {
The argument "orc8r_domain_name" is required, but no definition was found.
I’m seeing both errors specifying that the variable I’m setting in tfvars json is an undeclared variable and the required variable in variables.tf file is not defined.
What am i missing ? How should I properly assign the values to the required variables defined in variables.tf file through tfvars file ?
Any pointers here would be greatly useful.
…
Thanks
Karthik
It sounds like you have a module that requires the “orc8r_domain_name” variable, but the module block in main.tf isn’t supplying it.
If you are wanting to use a tfvars file that is for passing in variables to the root module, so you’d need to create a variable block (normally in variables.tf) and then pass that into the module (orc8r_domain_name = var.orc8r_domain_name).
1 Like
Thanks for the quick response here. Do you know how we can pass tfvars directly to a specific module instead of passing it to root module.
Hi @karthiksubraveti,
.tfvars
files are only for root module variables. The arguments inside the module
block are the only way to set variables for child modules.
One way to think about it is that the .tfvars
files together serve as a sort of virtual module
block for the root module, because the root module is being “called” directly by you at the command line, rather than from within the configuration.
To set that child module variable from a root module variable will require declaring it also as a root module variable and then passing it along inside the module
block, like this:
variable "orc8r_domain_name" {
type = string
}
module "orc8r-app" {
source = "./modules/orc8r-app"
orc8r_domain_name = var.orc8r_domain_name
# ...
}
I’ve shown the variable
block and the module
block together here for the ease of sharing the example snippet, but as @stuart-c noted it’s conventional to put variable
blocks in a file called variables.tf
and module calls in other files, so if you honor that convention then these two blocks would actually be in two separate files in your root module directory.
1 Like
Got it. Thanks @apparentlymart for helping me understand this better. I will structure my files this way.