terraform.tfvars and *.auto.tfvars are not loaded when a directory is given

Created on 27 Sep 2017  路  9Comments  路  Source: hashicorp/terraform

Version: 0.10.6
Debug output: https://pastebin.com/raw/a9jd3srL

What's wrong: The automatically loaded .tfvars files are never loaded.

Expected behavior: Automatically load terraform.tfvars and *.auto.tfvars in the given deploy directory.

terraform get deploy
terraform init deploy

# Neither of these work as expected.
terraform validate deploy
terraform plan deploy

deploy/main.tf:

variable "flavor_id" {}
variable "openstack_password" {}

deploy/terraform.tfvars:

flavor_id = 50

deploy/secret.auto.tfvars:

openstack_password = "xxxxxxxxxxxxxxx"
cli enhancement

Most helpful comment

@teamterraform and others. Looks like people are following the documentation without realising you need to still define the variable. When using a variable from a *.auto.tfvars and terraform.tfvars, it still has to be declared in your template as:

  • variable "SomeVariable" {}

I noticed a moment ago when following: https://www.terraform.io/docs/configuration/variables.html

This is present in Terraform v0.12.7

Though of course, the CLI tells you as such :)

Error: Reference to undeclared input variable

  on example.tf line 5, in provider "aws":
   5:   region     = var.SomeVariable

An input variable with the name "SomeVariable" has not been declared. This variable
can be declared with a variable "SomeVariable" {} block.

All 9 comments

This still appears to be a bug. +1 to prioritize it.

This is still a bug in 0.11.11

$ terraform -v
Terraform v0.12.3

bug is still here

Having this issue in v0.12.6 still

Hello,

This is currently the expected behavior of the CLI. Even when applying a directory argument, Terraform is still dependent on the current working directory, and uses that directory to search for various files and internal configuration.

To work with variables files in a subdirectory, you will need to use the -var-file= argument. Because of the reliance on the current working directory, we recommend having the root config be placed there, rather than in various subdirectories.

@teamterraform and others. Looks like people are following the documentation without realising you need to still define the variable. When using a variable from a *.auto.tfvars and terraform.tfvars, it still has to be declared in your template as:

  • variable "SomeVariable" {}

I noticed a moment ago when following: https://www.terraform.io/docs/configuration/variables.html

This is present in Terraform v0.12.7

Though of course, the CLI tells you as such :)

Error: Reference to undeclared input variable

  on example.tf line 5, in provider "aws":
   5:   region     = var.SomeVariable

An input variable with the name "SomeVariable" has not been declared. This variable
can be declared with a variable "SomeVariable" {} block.

This is currently the expected behavior of the CLI. Even when applying a directory argument, Terraform is still dependent on the current working directory, and uses that directory to search for various files and internal configuration.

This is not what I expected at all. I'm struggling to see why that should be the expected behaviour. It certainly isn't documented in that way.

The only time that I would give a directory argument is when the variables and other configuration are not in the current directory.

In what cases would someone have variables in one directory and provide a directory argument to another directory? This does not seem very normal. I would expect these to the cases where someone should be force to provide the var-file argument rather than the other way around.

This certainly still feels like a bug to me.

Usage: terraform plan [options] [DIR]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

This gives no mention of what the [DIR] argument does nor why it would only apply to *.tf files and not *.tfvars

  -var-file=foo       Set variables in the Terraform configuration from
                      a file. If "terraform.tfvars" or any ".auto.tfvars"
                      files are present, they will be automatically loaded.

This also does not say that they are only loaded from the current working directory.

At the very least consider this as a documentation bug.

This still exists in 0.13.5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darron picture darron  路  3Comments

shanmugakarna picture shanmugakarna  路  3Comments

zeninfinity picture zeninfinity  路  3Comments

ronnix picture ronnix  路  3Comments

rjinski picture rjinski  路  3Comments