hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.74k stars 9.56k forks source link

terraform plan/apply with "-var-file=<custom_value>"still loads terraform.tfvars if present #25129

Open lubo-p opened 4 years ago

lubo-p commented 4 years ago

Terraform Version

Terraform v0.12.25
+ provider.null v2.1.2

Terraform Configuration Files

main.tf:
resource null_resource test_resource {
  triggers = {
    val = var.test_val
  }
  provisioner "local-exec" {
    command = "echo ${var.test_val}"
  }
}

variables.tf:
variable test_val {
  type = string
}

custom.tfvars:
test_val1="custom"

terraform.tfvars:
test_val="default"

Debug Output

Crash Output

Expected Behavior

When running terraform plan or apply and providing custom value of -var-file and -state, the expectation is that only these specified files will be loaded.

Actual Behavior

According to my experiment if terraform.tfvars exists in the same directory and some value is missing in "custom" provided tfvar files, it will gets loaded from terrafrom.tfvars when such file should be not read at all.

Steps to Reproduce

$ terraform init $ terraform plan -var-file=./custom.tfvars -state=./custom.tfstate Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.


An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

null_resource.test_resource will be created

Plan: 1 to add, 0 to change, 0 to destroy.

Now if I delete the terraform.tfvars I get the expected behavior - asking for missing value: $ rm terraform.tfvars $ terraform plan -var-file=./custom.tfvars -state=./custom.tfstate var.test_val Enter a value:

Additional Context

References

apparentlymart commented 4 years ago

Hi @lubo-p,

Always loading the terraform.tfvars and *.auto.tfvars files is the intended behavior reflected in the documentation. -var and -var-file arguments are interpreted as overrides for those automatic files, if present.

There is not currently any way to tell Terraform via the command line not to process the automatic variables. The only way to stop them being processed is to rename them to not be recognized as automatically-included.

It seems this issue is implying a feature request for some way to tell Terraform to not automatically load these files, so I'm going to label it with that in mind. Please let me know if that wasn't what you intended.