terratest icon indicating copy to clipboard operation
terratest copied to clipboard

reorder of terraform arguments var and varFiles to reflect common usage

Open matt-smith opened this issue 6 years ago • 5 comments

Switching the order of Vars and VarFiles in terraformArgs to reflect a more common scenario.

It's likely that a user would like to override one or two variables from a var-file using single variables, currently the behaviour is the opposite way round.

link to terraform variable precedence documentation.

matt-smith avatar Mar 11 '19 08:03 matt-smith

Thanks for the PR! How did you test this? Could you add a test case that specifically verifies this behavior?

brikis98 avatar Mar 18 '19 10:03 brikis98

Hi I have just run into the same issue, given an expected order of vars and file-vars the expected precedence is not observed.

easy enough to set the variables 1 at a time, just a bit of duplication.

jimusbobus avatar Apr 06 '20 14:04 jimusbobus

we have come across a new for this as well. But this PR has been open for quite some time, is there any chance this will be reviewed and actioned upon soon ?

aldojimenez avatar Dec 09 '21 09:12 aldojimenez

I use -var-file to set the bulk of the variables, including defaults, and then would like to use -var to override them, for example to see what happens when a variable changes. This is currently not possible because changes provided via -var are clobbered by -var-file. Any chance you will accept this PR or provide an alternative soon?

Nuru avatar Feb 26 '22 02:02 Nuru

Also hoping that this gets merged soon

archetypalsxe avatar Aug 17 '22 19:08 archetypalsxe

@robmorgan there is a chance to merge this update?

pimielowski avatar Oct 17 '22 19:10 pimielowski

If you locate your terraform.tfvars file in the same directory as the Terraform module you apply terraform.InitAndApply, then Terraform will automatically pick up on the variables defined in that file. You can then just use Vars to override the values set in your terraform.tfvars file:

terraformOptions := &terraform.Options{
  TerraformDir: "../location/of/terraform/module/",
  Vars:         tfvarsOverridesGoHere,
}

tobias-edwards avatar Oct 21 '22 18:10 tobias-edwards

As far as I was checking, this PR is not needed, because #1217 resolves issue with precedence - if you define SetVarsAfterVarFiles: true, then values passed in Vars take precedence over values from VarFiles e.g.:

terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
	TerraformDir: ".",
	VarFiles:     []string{"example.tfvars"},
	Vars: map[string]interface{}{
		"example": "value_passed_from_test",
	},
	SetVarsAfterVarFiles: true,
})

sebastianczech avatar Jan 05 '23 10:01 sebastianczech

closing as resolved in #1217

matt-smith avatar Mar 08 '23 17:03 matt-smith